-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.java
41 lines (41 loc) · 1.22 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
class Solution {
public List<String> printKMoves(int K) {
int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
int[] dirs = {0, 1, 0, -1, 0};
String d = "RDLU";
int x = 0, y = 0, p = 0;
Set<List<Integer>> black = new HashSet<>();
while (K-- > 0) {
List<Integer> t = List.of(x, y);
if (black.add(t)) {
p = (p + 1) % 4;
} else {
black.remove(t);
p = (p + 3) % 4;
}
x += dirs[p];
y += dirs[p + 1];
x1 = Math.min(x1, x);
y1 = Math.min(y1, y);
x2 = Math.max(x2, x);
y2 = Math.max(y2, y);
}
int m = x2 - x1 + 1;
int n = y2 - y1 + 1;
char[][] g = new char[m][n];
for (char[] row : g) {
Arrays.fill(row, '_');
}
for (List<Integer> t : black) {
int i = t.get(0) - x1;
int j = t.get(1) - y1;
g[i][j] = 'X';
}
g[x - x1][y - y1] = d.charAt(p);
List<String> ans = new ArrayList<>();
for (char[] row : g) {
ans.add(String.valueOf(row));
}
return ans;
}
}