Skip to content

Commit 033ea9a

Browse files
authored
Create 05 December Shortest Path by Removing K walls
05 December Shortest Path by Removing K walls
1 parent 0591ddd commit 033ea9a

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
int shotestPath(vector<vector<int>> mat, int n, int m, int k) {
4+
vector<vector<int>> lives(n, vector<int> (m, -1));
5+
queue<vector<int>> q;
6+
// q-> row, col, k, dist
7+
q.push({0, 0, k, 0});
8+
9+
int dx[] = {-1, 0, 1, 0, -1};
10+
while(!q.empty()) {
11+
vector<int> temp = q.front();
12+
q.pop();
13+
14+
int row = temp[0];
15+
int col = temp[1];
16+
int clives = temp[2];
17+
int dist = temp[3];
18+
19+
if(row == n-1 && col == m-1) return dist;
20+
21+
if(mat[row][col] == 1) clives--;
22+
23+
for(int i=0; i<4; i++) {
24+
int nrow = row + dx[i];
25+
int ncol = col + dx[i+1];
26+
27+
if(nrow >= 0 && nrow < n && ncol >= 0 && ncol < m && lives[nrow][ncol] < clives) {
28+
q.push({nrow, ncol, clives, dist+1});
29+
lives[nrow][ncol] = clives;
30+
}
31+
}
32+
}
33+
return -1;
34+
}
35+
};

0 commit comments

Comments
 (0)