-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1091-ShortestPathInBinaryMatrix.java
More file actions
40 lines (32 loc) · 1.11 KB
/
1091-ShortestPathInBinaryMatrix.java
File metadata and controls
40 lines (32 loc) · 1.11 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
//Question Link - https://leetcode.com/problems/shortest-path-in-binary-matrix/
class Solution {
public int shortestPathBinaryMatrix(int[][] g) {
//base case
if(g[0][0] == 1){
return -1;
}
//using bfs
int m = g.length, n = g[0].length, c = 0;
Queue<int[]> nm = new LinkedList<>();
nm.offer(new int[] {0, 0, 1});
int d[][] = {{1,1},{-1,-1},{1,0},{-1,0},{0,1},{0,-1},{1,-1},{-1,1}};
while(!(nm.isEmpty())){
int l = nm.size();
while(l --> 0){
int f[] = nm.poll();
if(f[0] == m-1 && f[1] == n-1){
return f[2];
}
for(int k[]: d){
int i = f[0] + k[0];
int j = f[1] + k[1];
if(i>=0 && j>=0 && i<m && j<n && g[i][j] == 0){
nm.offer(new int[] {i, j, f[2]+1});
g[i][j] = 1;
}
}
}
}
return -1;
}
}