-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshortestPathinaGridwithObstaclesElimination.cpp
More file actions
37 lines (36 loc) · 1.2 KB
/
shortestPathinaGridwithObstaclesElimination.cpp
File metadata and controls
37 lines (36 loc) · 1.2 KB
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
// Source: https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination/
// Author: Miao Zhang
// Date: 2021-04-21
class Solution {
public:
int shortestPath(vector<vector<int>>& grid, int k) {
vector<vector<int>> dirs{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int m = grid.size();
int n = grid[0].size();
vector<int> seen(m * n, INT_MAX);
queue<tuple<int, int, int>> q;
q.emplace(0, 0, 0);
seen[0 * n + 0] = 0;
int steps = 0;
while (!q.empty()) {
int size = q.size();
while (size--) {
int i, j, val;
tie(i, j, val) = q.front();
q.pop();
if (i == m - 1 && j == n - 1) return steps;
for (auto d: dirs) {
int x = i + d[0];
int y = j + d[1];
if (x < 0 || x >= m || y < 0 || y >= n) continue;
int v = val + grid[x][y];
if (v >= seen[x * n + y] || v > k) continue;
seen[x * n + y] = v;
q.emplace(x, y, v);
}
}
steps++;
}
return -1;
}
};