forked from szl0072/Leetcode-Solution-Code
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDesignSnakeGame.java
70 lines (63 loc) · 1.73 KB
/
DesignSnakeGame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package leetcode;
import java.util.Deque;
import java.util.HashSet;
import java.util.LinkedList;
/**
* Project Name : Leetcode
* Package Name : leetcode
* File Name : DesignSnakeGame
* Creator : Edward
* Date : Jan, 2018
* Description : 353. Design Snake Game
*/
public class DesignSnakeGame {
HashSet<Integer> set; // 位置
Deque<Integer> deque;
int score;
int foodIndex;
int width;
int height;
int[][] food;
public DesignSnakeGame(int width, int height, int[][] food) {
this.width = width;
this.height = height;
this.food = food;
set = new HashSet<>();
deque = new LinkedList<>();
score = 0;
foodIndex = 0;
set.add(0);
deque.offerLast(0);
}
public int move(String direction) {
if (score == -1) {
return -1;
}
int rowHead = deque.peekFirst() / width;
int colHead = deque.peekFirst() % width;
switch (direction) {
case "U" : rowHead--;
break;
case "D" : rowHead++;
break;
case "L" : colHead--;
break;
default : colHead++;
}
int head = rowHead * width + colHead;
set.remove(deque.peekLast());
if (rowHead < 0 || rowHead == height || colHead < 0 || colHead == width || set.contains(head)) {
return score = -1;
}
set.add(head);
deque.offerFirst(head);
if (foodIndex < food.length && rowHead == food[foodIndex][0] && colHead == food[foodIndex][1]) {
foodIndex++;
++score;
set.add(deque.peekLast());
return score;
}
deque.pollLast();
return score;
}
}