forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShortest Path in a Grid with Obstacles Elimination.cpp
39 lines (36 loc) · 1.32 KB
/
Shortest Path in a Grid with Obstacles Elimination.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
class Solution {
public:
vector<vector<int>> directions{{-1,0},{1,0},{0,1},{0,-1}};
int shortestPath(vector<vector<int>>& grid, int k) {
int m = grid.size(),n = grid[0].size(),ans=0;
queue<vector<int>> q;
bool visited[m][n][k+1];
memset(visited,false,sizeof(visited));
q.push({0,0,k});
visited[0][0][k]=true;
while(!q.empty()) {
int size = q.size();
while(size--) {
auto p = q.front();
q.pop();
if(p[0]==m-1 && p[1]==n-1) return ans;
for(auto x : directions) {
int i= p[0]+x[0];
int j= p[1]+x[1];
int obstacle = p[2];
if(i>=0 && i<m && j>=0 && j<n) {
if(grid[i][j]==0 && !visited[i][j][obstacle]) {
q.push({i,j,obstacle});
visited[i][j][obstacle] =true;
} else if(grid[i][j]==1 && obstacle>0 && !visited[i][j][obstacle-1]) {
q.push({i,j,obstacle-1});
visited[i][j][obstacle-1]=true;
}
}
}
}
ans++;
}
return -1;
}
};