-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshortest-path-in-binary-matrix.java
More file actions
57 lines (43 loc) · 1.4 KB
/
shortest-path-in-binary-matrix.java
File metadata and controls
57 lines (43 loc) · 1.4 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
class Solution {
private int[] dx = {0, 1, 1, 1, 0, -1, -1, -1};
private int[] dy = {1, 1, 0, -1, -1, -1, 0, 1};
private boolean[][] visited;
private int N;
public int shortestPathBinaryMatrix(int[][] grid) {
N = grid.length;
visited = new boolean[N][N];
if (grid[0][0] == 1) return -1;
int answer = bfs(grid);
return answer;
}
private int bfs(int[][] grid) {
Queue<Cell> q = new LinkedList<>();
q.add(new Cell(0, 0, 0));
visited[0][0] = true;
while(!q.isEmpty()) {
Cell p = q.poll();
if (p.x == N-1 && p.y == N-1) return p.step + 1;
for (int i = 0; i < 8; i++) {
int nx = dx[i] + p.x;
int ny = dy[i] + p.y;
if (isOut(nx) || isOut(ny)) continue;
if (grid[ny][nx] == 1) continue;
if (visited[ny][nx]) continue;
q.add(new Cell(nx, ny, p.step + 1));
visited[ny][nx] = true;
}
}
return -1;
}
private boolean isOut(int i) {
return i >= N || i < 0;
}
static class Cell {
int x, y, step;
public Cell(int x, int y, int s) {
this.x = x;
this.y = y;
this.step = s;
}
}
}