Google
353 Design Snake Game

1. Question

Design a Snake game that is played on a device with screen size =width x height. Play the game onlineif you are not familiar with the game.
The snake is initially positioned at the top left corner (0,0) with length = 1 unit.
You are given a list of food's positions in row-column order. When a snake eats the food, its length and the game's score both increase by 1.
Each food appears one by one on the screen. For example, the second food will not appear until the first food was eaten by the snake.
When a food does appear on the screen, it is guaranteed that it will not appear on a block occupied by the snake.
Example:
1
Given width = 3, height = 2, and food = [[1,2],[0,1]].
2
3
Snake snake = new Snake(width, height, food);
4
5
Initially the snake appears at position (0,0) and the food at (1,2).
6
7
|S| | |
8
| | |F|
9
10
snake.move("R"); -
11
>
12
Returns 0
13
14
| |S| |
15
| | |F|
16
17
snake.move("D"); -
18
>
19
Returns 0
20
21
| | | |
22
| |S|F|
23
24
snake.move("R"); -
25
>
26
Returns 1 (Snake eats the first food and right after that, the second food appears at (0,1) )
27
28
| |F| |
29
| |S|S|
30
31
snake.move("U"); -
32
>
33
Returns 1
34
35
| |F|S|
36
| | |S|
37
38
snake.move("L"); -
39
>
40
Returns 2 (Snake eats the second food)
41
42
| |S|S|
43
| | |S|
44
45
snake.move("U"); -
46
>
47
Returns -1 (Game over because snake collides with border)
Copied!

2. Implementation

思路:
1
class SnakeGame {
2
Set<Integer> set;
3
Deque<Integer> deque;
4
int score;
5
int foodIndex;
6
int width;
7
int height;
8
int[][] food;
9
10
/** Initialize your data structure here.
11
@param width - screen width
12
@param height - screen height
13
@param food - A list of food positions
14
E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0]. */
15
public SnakeGame(int width, int height, int[][] food) {
16
this.width = width;
17
this.height = height;
18
this.food = food;
19
this.score = 0;
20
this.foodIndex = 0;
21
set = new HashSet<>();
22
deque = new LinkedList<>();
23
deque.addLast(0);
24
}
25
26
/** Moves the snake.
27
@param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down
28
@return The game's score after the move. Return -1 if game over.
29
Game over when snake crosses the screen boundary or bites its body. */
30
public int move(String direction) {
31
if (score == -1) {
32
return -1;
33
}
34
35
int row = deque.peekFirst() / width;
36
int col = deque.peekFirst() % width;
37
38
switch(direction) {
39
case "U":
40
--row;
41
break;
42
case "L":
43
--col;
44
break;
45
case "D":
46
++row;
47
break;
48
case "R":
49
++col;
50
break;
51
}
52
53
int head = row * width + col;
54
int tail = deque.peekLast();
55
// temporarily remove tail so that snake's head can move to its last tail
56
set.remove(tail);
57
58
// When snake is out of bound or it touches its body, game is over
59
if (row < 0 || col < 0 || row >= height || col >= width || set.contains(head)) {
60
score = -1;
61
return score;
62
}
63
64
set.add(head);
65
deque.addFirst(head);
66
67
if (foodIndex < food.length && row == food[foodIndex][0] && col == food[foodIndex][1]) {
68
++score;
69
++foodIndex;
70
// 吃食物之后长度会加1,所以把tail放回set里
71
set.add(tail);
72
return score;
73
}
74
75
deque.removeLast();
76
return score;
77
}
78
}
79
80
/**
81
* Your SnakeGame object will be instantiated and called as such:
82
* SnakeGame obj = new SnakeGame(width, height, food);
83
* int param_1 = obj.move(direction);
84
*/
Copied!

3. Time & Space Complexity

move(): 时间复杂度O(1), 空间复杂度O(height * width)