-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2812.FindTheSafestPathInAGrid.cpp
More file actions
75 lines (64 loc) · 2.32 KB
/
2812.FindTheSafestPathInAGrid.cpp
File metadata and controls
75 lines (64 loc) · 2.32 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class Solution {
public:
int maximumSafenessFactor(vector<vector<int>>& grid) {
// Speed thingies.
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// Variables.
const int n = grid.size();
const auto inBounds = [n](const int x, const int y) {
return x >= 0 && y >= 0 && x < n && y < n;
};
static constexpr int checks[][2] = {
{ -1, 0 }, { 1, 0 },
{ 0, -1 }, { 0, 1 },
};
// Init tile scores.
deque<tuple<int, int, int>> tileQueue;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (grid[j][i] == 1) {
grid[j][i] = 0;
tileQueue.emplace_back(i, j, grid[j][i]);
} else {
grid[j][i] = -1;
}
}
}
// Generate scores.
while (!tileQueue.empty()) {
const auto [x, y, score] = tileQueue.front();
tileQueue.pop_front();
for (int k = 0; k < sizeof(checks) / sizeof(*checks); k++) {
const int cx = x + checks[k][0], cy = y + checks[k][1];
if (inBounds(cx, cy) && grid[cy][cx] == -1) {
tileQueue.emplace_back(cx, cy, score + 1);
grid[cy][cx] = score + 1;
}
}
}
// Path finding.
int minSafety = grid[0][0];
tileQueue.emplace_back(0, 0, minSafety);
while (!tileQueue.empty()) {
const auto [ x, y, safety ] = tileQueue.front();
tileQueue.pop_front();
minSafety = min(minSafety, safety);
if (x == n - 1 && y == n - 1) break;
for (int k = 0; k < sizeof(checks) / sizeof(*checks); k++) {
const int cx = x + checks[k][0], cy = y + checks[k][1];
if (inBounds(cx, cy) && grid[cy][cx] >= 0) {
const int cScore = grid[cy][cx];
if (cScore >= minSafety) {
tileQueue.emplace_front(cx, cy, cScore);
} else {
tileQueue.emplace_back(cx, cy, cScore);
}
grid[cy][cx] = -1;
}
}
}
return minSafety;
}
};