-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.java
32 lines (32 loc) · 1.12 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
class Solution {
public int shortestPathBinaryMatrix(int[][] grid) {
int n = grid.length;
if (grid[0][0] == 1 || grid[n - 1][n - 1] == 1) {
return -1;
}
Queue<int[]> queue = new ArrayDeque<>();
boolean[][] vis = new boolean[n][n];
queue.offer(new int[]{0, 0});
vis[0][0] = true;
int[][] dirs = new int[][]{{0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}};
int res = 1;
while (!queue.isEmpty()) {
int size = queue.size();
while (size-- != 0) {
int[] cur = queue.poll();
if (cur[0] == n - 1 && cur[1] == n - 1) {
return res;
}
for (int[] dir : dirs) {
int x = cur[0] + dir[0], y = cur[1] + dir[1];
if (x >= 0 && x < n && y >= 0 && y < n && !vis[x][y] && grid[x][y] == 0) {
vis[x][y] = true;
queue.offer(new int[]{x, y});
}
}
}
++res;
}
return -1;
}
}