-
Notifications
You must be signed in to change notification settings - Fork 16
/
ShortestPathInBinaryMatrix.java
64 lines (55 loc) · 1.93 KB
/
ShortestPathInBinaryMatrix.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
// https://leetcode.com/problems/shortest-path-in-binary-matrix
// N = total number of vertices
// T: O(N)
// S: O(N)
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class ShortestPathInBinaryMatrix {
private static final List<List<Integer>> directions = List.of(
List.of(-1, -1),
List.of(-1, 0),
List.of(-1, 1),
List.of(1, -1),
List.of(1, 0),
List.of(1, 1),
List.of(0, -1),
List.of(0, 1)
);
public int shortestPathBinaryMatrix(int[][] grid) {
final int n = grid.length;
if (grid[0][0] == 1 || grid[n - 1][n - 1] == 1) {
return -1;
}
final Queue<int[]> queue = new LinkedList<>();
queue.add(new int[] {0, 0});
grid[0][0] = 1;
while (!queue.isEmpty()) {
final int[] position = queue.poll();
if (position[0] == n - 1 && position[1] == n - 1) {
return grid[n - 1][n - 1];
}
for (int[] neighbour : getNeighbours(position, grid)) {
queue.add(neighbour);
grid[neighbour[0]][neighbour[1]] = grid[position[0]][position[1]] + 1;
}
}
return -1;
}
private static List<int[]> getNeighbours(int[] position, int[][] grid) {
final List<int[]> result = new ArrayList<>();
for (List<Integer> direction : directions) {
final int row = position[0] + direction.get(0);
final int column = position[1] + direction.get(1);
if (isValid(grid, row, column) && grid[row][column] == 0) {
result.add(new int[] { row, column });
}
}
return result;
}
private static boolean isValid(int[][] grid, int row, int column) {
final int n = grid.length;
return row >= 0 && row < n && column >= 0 && column < n;
}
}