-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathTheMazeII505.java
354 lines (330 loc) · 11.6 KB
/
TheMazeII505.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/**
* There is a ball in a maze with empty spaces and walls. The ball can go
* through empty spaces by rolling up, down, left or right, but it won't stop
* rolling until hitting a wall. When the ball stops, it could choose the next
* direction.
*
* Given the ball's start position, the destination and the maze, find the
* shortest distance for the ball to stop at the destination. The distance is
* defined by the number of empty spaces traveled by the ball from the start
* position (excluded) to the destination (included). If the ball cannot stop
* at the destination, return -1.
*
* The maze is represented by a binary 2D array. 1 means the wall and 0 means
* the empty space. You may assume that the borders of the maze are all walls.
* The start and destination coordinates are represented by row and column
* indexes.
*
* Example 1
*
* Input 1: a maze represented by a 2D array
*
* 0 0 1 0 0
* 0 0 0 0 0
* 0 0 0 1 0
* 1 1 0 1 1
* 0 0 0 0 0
*
* Input 2: start coordinate (rowStart, colStart) = (0, 4)
* Input 3: destination coordinate (rowDest, colDest) = (4, 4)
*
* Output: 12
* Explanation: One shortest way is : left -> down -> left -> down -> right -> down -> right.
* The total distance is 1 + 1 + 3 + 1 + 2 + 2 + 2 = 12.
* https://leetcode.com/static/images/problemset/maze_1_example_1.png
*
*
* Example 2
*
* Input 1: a maze represented by a 2D array
*
* 0 0 1 0 0
* 0 0 0 0 0
* 0 0 0 1 0
* 1 1 0 1 1
* 0 0 0 0 0
*
* Input 2: start coordinate (rowStart, colStart) = (0, 4)
* Input 3: destination coordinate (rowDest, colDest) = (3, 2)
*
* Output: -1
* Explanation: There is no way for the ball to stop at the destination.
* https://leetcode.com/static/images/problemset/maze_1_example_2.png
*
* Note:
* There is only one ball and one destination in the maze.
* Both the ball and the destination exist on an empty space, and they will not
* be at the same position initially.
* The given maze does not contain border (like the red rectangle in the
* example pictures), but you could assume the border of the maze are all walls.
* The maze contains at least 2 empty spaces, and both the width and height of
* the maze won't exceed 100.
*/
public class TheMazeII505 {
private int[][] DIRECTIONS = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
public int shortestDistance(int[][] maze, int[] start, int[] destination) {
int M = maze.length;
int N = maze[0].length;
if (isSame(start, destination)) return 0;
int[][] visited = new int[M][N];
for (int[] row: visited) {
Arrays.fill(row, -1);
}
Queue<Point> q = new LinkedList<>();
q.add(new Point(start, 0));
visited[start[0]][start[1]] = 0;
int min = Integer.MAX_VALUE;
while (!q.isEmpty()) {
int size = q.size();
Point curr = q.poll();
if (isSame(curr.pos, destination)) {
min = Math.min(min, curr.dist);
continue;
}
for (int[] d: DIRECTIONS) {
int x = curr.pos[0] + d[0];
int y = curr.pos[1] + d[1];
int more = 1;
while (x >= 0 && y >= 0 && x < M && y < N && maze[x][y] == 0) {
x += d[0];
y += d[1];
more++;
}
x -= d[0];
y -= d[1];
more--;
int[] newPos = new int[]{x, y};
if (visited[x][y] == -1 || visited[x][y] > curr.dist + more) {
q.add(new Point(newPos, curr.dist + more));
visited[x][y] = curr.dist + more;
}
}
}
return min == Integer.MAX_VALUE ? -1 : min;
}
private boolean isSame(int[] a, int[] b) {
return a[0] == b[0] && a[1] == b[1];
}
class Point {
int[] pos;
int dist;
Point(int[] p, int d) {
pos = p;
dist = d;
}
}
/**
* https://leetcode.com/problems/the-maze-ii/discuss/98418/Simple-Java-Solution-BFS
*/
public int shortestDistance2(int[][] maze, int[] start, int[] destination) {
Queue<int[]> q = new LinkedList<>();
int m = maze.length, n = maze[0].length;
int[][] dist = new int[m][n];
for (int i = 0; i < m; i++) {
Arrays.fill(dist[i], Integer.MAX_VALUE);
}
int[] dx = new int[] {-1, 0, 1, 0};
int[] dy = new int[] { 0, 1, 0, -1};
q.offer(start);
dist[start[0]][start[1]] = 0;
while (!q.isEmpty()) {
int[] p = q.poll();
for (int i = 0; i < 4; i++) {
int x = p[0] + dx[i], y = p[1] + dy[i];
int cnt = 1;
while (x >=0 && x < m && y >= 0 && y < n && maze[x][y] != 1) {
x += dx[i];
y += dy[i];
cnt++;
}
x -= dx[i];
y -= dy[i];
cnt--;
if (dist[p[0]][p[1]] + cnt < dist[x][y]) {
dist[x][y] = dist[p[0]][p[1]] + cnt;
q.offer(new int[] {x, y});
}
}
}
return dist[destination[0]][destination[1]] == Integer.MAX_VALUE ? -1 : dist[destination[0]][destination[1]];
}
public int shortestDistance3(int[][] maze, int[] start, int[] destination) {
int M = maze.length;
int N = maze[0].length;
int[][] memo = new int[M][N];
for (int[] row: memo) Arrays.fill(row, Integer.MAX_VALUE);
memo[start[0]][start[1]] = 0;
roll(maze, start[0], start[1], memo, M, N);
return memo[destination[0]][destination[1]] == Integer.MAX_VALUE ? -1 : memo[destination[0]][destination[1]];
}
private void roll(int[][] maze, int i, int j, int[][] memo, int M, int N) {
// top
int t = i - 1;
int rt = 1;
while (t >= 0 && maze[t][j] == 0) {
t--;
rt++;
}
t++;
rt--;
if (memo[i][j] + rt < memo[t][j]) {
memo[t][j] = memo[i][j] + rt;
roll(maze, t, j, memo, M, N);
}
// bottom
int b = i + 1;
int rb = 1;
while (b < M && maze[b][j] == 0) {
b++;
rb++;
}
b--;
rb--;
if (memo[i][j] + rb < memo[b][j]) {
memo[b][j] = memo[i][j] + rb;
roll(maze, b, j, memo, M, N);
}
// left
int l = j - 1;
int rl = 1;
while (l >= 0 && maze[i][l] == 0) {
l--;
rl++;
}
l++;
rl--;
if (memo[i][j] + rl < memo[i][l]) {
memo[i][l] = memo[i][j] + rl;
roll(maze, i, l, memo, M, N);
}
// right
int r = j + 1;
int rr = 1;
while (r < N && maze[i][r] == 0) {
r++;
rr++;
}
r--;
rr--;
if (memo[i][j] + rr < memo[i][r]) {
memo[i][r] = memo[i][j] + rr;
roll(maze, i, r, memo, M, N);
}
}
/**
* https://leetcode.com/problems/the-maze-ii/solution/
*/
public int shortestDistance4(int[][] maze, int[] start, int[] dest) {
int[][] distance = new int[maze.length][maze[0].length];
for (int[] row: distance)
Arrays.fill(row, Integer.MAX_VALUE);
distance[start[0]][start[1]] = 0;
dfs(maze, start, distance);
return distance[dest[0]][dest[1]] == Integer.MAX_VALUE ? -1 : distance[dest[0]][dest[1]];
}
public void dfs(int[][] maze, int[] start, int[][] distance) {
int[][] dirs={{0,1}, {0,-1}, {-1,0}, {1,0}};
for (int[] dir: dirs) {
int x = start[0] + dir[0];
int y = start[1] + dir[1];
int count = 0;
while (x >= 0 && y >= 0 && x < maze.length && y < maze[0].length && maze[x][y] == 0) {
x += dir[0];
y += dir[1];
count++;
}
if (distance[start[0]][start[1]] + count < distance[x - dir[0]][y - dir[1]]) {
distance[x - dir[0]][y - dir[1]] = distance[start[0]][start[1]] + count;
dfs(maze, new int[]{x - dir[0],y - dir[1]}, distance);
}
}
}
/**
* https://leetcode.com/problems/the-maze-ii/solution/
*/
public int shortestDistance5(int[][] maze, int[] start, int[] destination) {
int M = maze.length;
int N = maze[0].length;
int[][] distances = new int[M][N];
for (int[] row: distances) Arrays.fill(row, Integer.MAX_VALUE);
distances[start[0]][start[1]] = 0;
boolean[][] visited = new boolean[M][N];
roll(maze, distances, visited, M, N);
int res = distances[destination[0]][destination[1]];
return res == Integer.MAX_VALUE ? -1 : res;
}
private void roll(int[][] maze, int[][] distances, boolean[][] visited, int M, int N) {
while (true) {
int[] m = minDistance(distances, visited, M, N);
if (m == null) return;
visited[m[0]][m[1]] = true;
for (int[] dir: DIRECTIONS) {
int x = m[0] + dir[0];
int y = m[1] + dir[1];
int dis = 0;
while (x >= 0 && y >= 0 && x < M && y < N && maze[x][y] == 0) {
x += dir[0];
y += dir[1];
dis++;
}
x -= dir[0];
y -= dir[1];
int d = distances[m[0]][m[1]] + dis;
if (d < distances[x][y]) {
distances[x][y] = d;
}
}
}
}
private int[] minDistance(int[][] distances, boolean[][] visited, int M, int N) {
int[] res = null;
int min = Integer.MAX_VALUE;
for (int i=0; i<M; i++) {
for (int j=0; j<N; j++) {
if (!visited[i][j] && distances[i][j] < min) {
res = new int[]{i, j};
min = distances[i][j];
}
}
}
return res;
}
/**
* https://leetcode.com/problems/the-maze-ii/solution/
*/
public int shortestDistance6(int[][] maze, int[] start, int[] destination) {
int M = maze.length;
int N = maze[0].length;
int[][] distances = new int[M][N];
for (int[] row: distances) Arrays.fill(row, Integer.MAX_VALUE);
distances[start[0]][start[1]] = 0;
roll(maze, distances, start, M, N);
int res = distances[destination[0]][destination[1]];
return res == Integer.MAX_VALUE ? -1 : res;
}
private void roll(int[][] maze, int[][] distances, int[] start, int M, int N) {
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[2] - b[2]);
pq.add(new int[]{start[0], start[1], 0});
while (!pq.isEmpty()) {
int[] md = pq.poll();
if (distances[md[0]][md[1]] < md[2]) continue;
for (int[] dir: DIRECTIONS) {
int x = md[0] + dir[0];
int y = md[1] + dir[1];
int dis = 0;
while (x >= 0 && y >= 0 && x < M && y < N && maze[x][y] == 0) {
x += dir[0];
y += dir[1];
dis++;
}
x -= dir[0];
y -= dir[1];
int d = distances[md[0]][md[1]] + dis;
if (d < distances[x][y]) {
distances[x][y] = d;
pq.add(new int[]{x, y, d});
}
}
}
}
}