-
Notifications
You must be signed in to change notification settings - Fork 0
/
Octomber_30.cpp
54 lines (44 loc) · 1.34 KB
/
Octomber_30.cpp
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
// https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination/description/
// 1293. Shortest Path in a Grid with Obstacles Elimination
class Solution {
public:
int shortestPath(vector<vector<int>>& grid, int k) {
int n = grid.size();
int m = grid[0].size();
vector<vector<int>>vis(n,vector<int>(m,-1));
queue<vector<int>>q;
q.push({0,0,0,k});
while(!q.empty()){
auto t = q.front();
int x = t[0];
int y = t[1];
q.pop();
// if current position is outside the grid
if(x<0 || y<0 || x>=n || y>=m ){
continue;
}
// destination found
if(x == n-1 and y == m-1){
return t[2];
}
if(grid[x][y]==1){
if(t[3]>0){
t[3]--;
}
else{
continue;
}
}
// add neighbour possibility
if(vis[x][y]!=-1 and vis[x][y]>=t[3]){
continue;
}
vis[x][y] = t[3];
q.push({x+1,y,t[2]+1,t[3]});
q.push({x,y+1,t[2]+1,t[3]});
q.push({x-1,y,t[2]+1,t[3]});
q.push({x,y-1,t[2]+1,t[3]});
}
return -1;
}
};