-
Notifications
You must be signed in to change notification settings - Fork 277
/
ShortestPathInAGird.java
51 lines (44 loc) · 1.31 KB
/
ShortestPathInAGird.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
class Solution {
private class Point{
private int x;
private int y;
public Point(int x, int y){
this.x = x;
this.y = y;
}
}
public int shortestPathBinaryMatrix(int[][] grid) {
int n = grid.length -1;
if(grid[0][0] ==1 || grid[n][n] ==1){
return -1;
}
int level = 0;
Queue<Point> qu =new LinkedList<>();
qu.offer(new Point(0,0));
while(!qu.isEmpty()){
int size= qu.size();
while(size-->0){
Point head = qu.poll();
int x = head.x;
int y = head.y;
if(x == n && y ==n ){
return level+1;
}
if(x < 0 || y<0 || x>n || y >n || grid[x][y] >=1){
continue;
}
grid[x][y] = 2;
qu.offer(new Point(x-1, y));
qu.offer(new Point(x+1, y));
qu.offer(new Point(x, y-1));
qu.offer(new Point(x, y+1));
qu.offer(new Point(x-1, y-1));
qu.offer(new Point(x-1, y+1));
qu.offer(new Point(x+1, y-1));
qu.offer(new Point(x+1, y+1));
}
level++;
}
return -1;
}
}