-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
45 lines (41 loc) · 1.3 KB
/
Solution.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
package L874;
import java.util.HashSet;
import java.util.Set;
public class Solution {
public static int robotSim(int[] commands, int[][] obstacles) {
int[][] dirs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int px = 0, py = 0, d = 1;
Set<Integer> set = new HashSet<Integer>();
for (int[] obstacle : obstacles) {
set.add(obstacle[0] * 60001 + obstacle[1]);
}
int res = 0;
for (int c : commands) {
if (c < 0) {
d += c == -1 ? 1 : -1;
d %= 4;
if (d < 0) {
d += 4;
}
} else {
for (int i = 0; i < c; i++) {
if (set.contains((px + dirs[d][0]) * 60001 + py + dirs[d][1])) {
break;
}
px += dirs[d][0];
py += dirs[d][1];
res = Math.max(res, px * px + py * py);
}
}
}
return res;
}
public static void main(String[] args) {
// int[] commands = {4, -1, 3};
// int[][] obstacles = {};
int[] commands = {4, -1, 4, -2, 4};
int[][] obstacles = {{2, 4}};
int res = robotSim(commands, obstacles);
System.out.println(res);
}
}