-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3286.cpp
More file actions
27 lines (27 loc) · 963 Bytes
/
Copy path3286.cpp
File metadata and controls
27 lines (27 loc) · 963 Bytes
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
class Solution {
public:
vector<pair<int, int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
bool findSafeWalk(vector<vector<int>>& grid, int health) {
priority_queue<pair<int, pair<int, int>>> pq;
int m = grid.size();
int n = grid[0].size();
pq.push({-grid[0][0], {0, 0}});
grid[0][0] = -1;
while (!pq.empty()) {
auto [v, p] = pq.top();
auto [x, y] = p;
pq.pop();
if (x == m - 1 && y == n - 1) return health >= 1 - v;
for (auto& direction : directions) {
int xx = x + direction.first;
int yy = y + direction.second;
if (xx < 0 || xx >= m || yy < 0 || yy >= n) continue;
if (grid[xx][yy] == -1) continue;
int vv = v - grid[xx][yy];
grid[xx][yy] = -1;
pq.push({vv, {xx, yy}});
}
}
return false;
}
};