forked from ex01tus/leetcode-grind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortestPathInBinaryMatrix.java
More file actions
57 lines (46 loc) · 1.71 KB
/
ShortestPathInBinaryMatrix.java
File metadata and controls
57 lines (46 loc) · 1.71 KB
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
package graph;
import java.util.LinkedList;
import java.util.Queue;
/**
* Description: https://leetcode.com/problems/shortest-path-in-binary-matrix
* Difficulty: Medium
* Time complexity: O(m * n)
* Space complexity: O(m * n)
*/
public class ShortestPathInBinaryMatrix {
private final int[][] directions = new int[][]{
{1, 0}, {-1, 0}, {0, 1}, {0, -1},
{1, 1}, {1, -1}, {-1, 1}, {-1, -1} // diagonal movement is allowed
};
public int shortestPathBinaryMatrix(int[][] grid) {
if (grid[0][0] == 1 || grid[grid.length - 1][grid[0].length - 1] == 1) {
return -1;
}
int[][] visited = new int[grid.length][grid[0].length];
Queue<int[]> planned = new LinkedList<>();
planned.offer(new int[]{grid.length - 1, grid[0].length - 1});
visited[grid.length - 1][grid[0].length - 1] = 1;
int pathLength = 0;
while (!planned.isEmpty()) {
int levelSize = planned.size();
pathLength++;
for (int i = 0; i < levelSize; i++) {
int[] current = planned.poll();
if (current[0] == 0 && current[1] == 0) {
return pathLength;
}
for (int[] dir : directions) {
int x = current[0] + dir[0];
int y = current[1] + dir[1];
if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length
&& grid[x][y] == 0
&& visited[x][y] == 0) {
planned.offer(new int[]{x, y});
visited[x][y] = 1;
}
}
}
}
return -1;
}
}