-
Notifications
You must be signed in to change notification settings - Fork 277
/
Copy pathShortestPathinaGridwithObstaclesElimination.java
57 lines (47 loc) · 1.9 KB
/
ShortestPathinaGridwithObstaclesElimination.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
class Solution {
private int[][] dir = new int[][]{{0,1},{0,-1},{-1,0},{1,0}};
public int shortestPath(int[][] grid, int k) {
int m = grid.length;
int n = grid[0].length;
if(m == 0 || n == 0) return 0;
// min Number of obstacles encountered at each cell
int[][] obstacle = new int[m][n];
boolean[][] visited = new boolean[m][n];
Queue<int[]> queue = new LinkedList<>();
// x,y,obstacleCount
queue.add(new int[]{0, 0, grid[0][0]});
obstacle[0][0] = grid[0][0];
int level = 1;
while(!queue.isEmpty()) {
int size = queue.size();
while(size-->0) {
int[] head = queue.poll();
if(head[0] == m-1 && head[1] == n-1) {
return level - 1;
}
// head node this is ObstacleCount
int currObstacleCount = head[2];
for(int[] d : dir) {
int x = head[0]+d[0];
int y = head[1]+d[1];
// within limits
if(!(x >= 0 && y >= 0 && x < m && y < n)) continue;
int oldObstacleCount = obstacle[x][y];
int newObstacleCount = currObstacleCount + grid[x][y];
if ((!visited[x][y]) && newObstacleCount <= k ) {
queue.add(new int[] {x, y, newObstacleCount});
obstacle[x][y] = newObstacleCount;
visited[x][y] = true;
}
if ((oldObstacleCount > newObstacleCount ) && newObstacleCount <= k ) {
queue.add(new int[] {x, y, newObstacleCount});
obstacle[x][y] = newObstacleCount;
visited[x][y] = true;
}
}
}
level++;
}
return -1;
}
}