-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
find-a-safe-walk-through-a-grid.cpp
41 lines (40 loc) · 1.5 KB
/
find-a-safe-walk-through-a-grid.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
// Time: O(m * n)
// Space: O(m * n)
// 0-1 bfs, deque
class Solution {
public:
bool findSafeWalk(vector<vector<int>>& grid, int health) {
static const vector<pair<int, int>> directions = {{0, 1}, {0, -1},
{1, 0}, {-1, 0}};
const pair<int, int> b = {0, 0}, t = {size(grid) - 1, size(grid[0]) - 1};
if (!(0 + grid[0][0] < health)) {
return false;
}
deque<pair<pair<int, int>, int>> dq = {{b, grid[0][0]}};
unordered_set<int> lookup;
while (!empty(dq)) {
const auto [b, d] = dq.front(); dq.pop_front();
if (b == t) {
return true;
}
if (lookup.count(b.first * size(grid[0]) + b.second)) {
continue;
}
lookup.emplace(b.first * size(grid[0]) + b.second);
for (const auto& [dr, dc] : directions) {
const auto& nb = make_pair(b.first + dr, b.second + dc);
if (!(0 <= nb.first && nb.first < size(grid) &&
0 <= nb.second && nb.second < size(grid[0]) &&
!lookup.count(nb.first * size(grid[0]) + nb.second))) {
continue;
}
if (!grid[nb.first][nb.second]) {
dq.emplace_front(nb, d);
} else if (d + 1 < health) {
dq.emplace_back(nb, d + 1);
}
}
}
return false;
}
};