-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
find-the-safest-path-in-a-grid.cpp
234 lines (222 loc) · 8.2 KB
/
find-the-safest-path-in-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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Time: O(n^2)
// Space: O(n^2)
// bfs, bucket sort, union find
class Solution {
public:
int maximumSafenessFactor(vector<vector<int>>& grid) {
static const vector<pair<int, int>> DIRECTIONS = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const auto& bfs = [&]() {
vector<vector<int>> dist(size(grid), vector<int>(size(grid[0]), -1));
vector<pair<int, int>> q;
for (int r = 0; r < size(grid); ++r) {
for (int c = 0; c < size(grid[0]); ++c) {
if (grid[r][c]) {
dist[r][c] = 0;
q.emplace_back(r, c);
}
}
}
int d = 0;
for (int d = 0; !empty(q); ++d) {
vector<pair<int, int>> new_q;
for (const auto& [r, c] : q) {
for (const auto& [dr, dc] : DIRECTIONS) {
const int nr = r + dr, nc = c + dc;
if (!(0 <= nr && nr < size(dist) && 0 <= nc && nc < size(dist[0]) && dist[nr][nc] == -1)) {
continue;
}
dist[nr][nc] = d + 1;
new_q.emplace_back(nr, nc);
}
}
q = move(new_q);
}
return dist;
};
auto dist = bfs();
vector<vector<pair<int, int>>> buckets((size(grid) - 1) + (size(grid[0]) - 1) + 1);
for (int r = 0; r < size(grid); ++r) {
for (int c = 0; c < size(grid[0]); ++c) {
buckets[dist[r][c]].emplace_back(r, c);
}
}
vector<vector<bool>> lookup(size(grid), vector<bool>(size(grid[0])));
UnionFind uf(size(grid) * size(grid[0]));
int d = size(buckets) - 1;
for (; d >= 0; --d) {
for (const auto& [r, c] : buckets[d]) {
for (const auto& [dr, dc] : DIRECTIONS) {
const int nr = r + dr, nc = c + dc;
if (!(0 <= nr && nr < size(dist) && 0 <= nc && nc < size(dist[0]) && lookup[nr][nc])) {
continue;
}
uf.union_set(nr * size(grid[0]) + nc, r * size(grid[0]) + c);
}
lookup[r][c] = true;
}
if (uf.find_set(0 * size(grid[0]) + 0) == uf.find_set((size(grid) - 1) * size(grid[0]) + (size(grid[0]) - 1))) {
break;
}
}
return d;
}
private:
class UnionFind {
public:
UnionFind(int n)
: set_(n)
, rank_(n) {
iota(begin(set_), end(set_), 0);
}
int find_set(int x) {
if (set_[x] != x) {
set_[x] = find_set(set_[x]); // Path compression.
}
return set_[x];
}
bool union_set(int x, int y) {
x = find_set(x), y = find_set(y);
if (x == y) {
return false;
}
if (rank_[x] > rank_[y]) {
swap(x, y);
}
set_[x] = y; // Union by rank.
if (rank_[x] == rank_[y]) {
++rank_[y];
}
return true;
}
private:
vector<int> set_;
vector<int> rank_;
};
};
// Time: O(n^2 * logn)
// Space: O(n^2)
// bfs, dijkstra's algorithm
class Solution2 {
public:
int maximumSafenessFactor(vector<vector<int>>& grid) {
static const vector<pair<int, int>> DIRECTIONS = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const auto& bfs = [&]() {
vector<vector<int>> dist(size(grid), vector<int>(size(grid[0]), -1));
vector<pair<int, int>> q;
for (int r = 0; r < size(grid); ++r) {
for (int c = 0; c < size(grid[0]); ++c) {
if (grid[r][c]) {
dist[r][c] = 0;
q.emplace_back(r, c);
}
}
}
int d = 0;
for (int d = 0; !empty(q); ++d) {
vector<pair<int, int>> new_q;
for (const auto& [r, c] : q) {
for (const auto& [dr, dc] : DIRECTIONS) {
const int nr = r + dr, nc = c + dc;
if (!(0 <= nr && nr < size(dist) && 0 <= nc && nc < size(dist[0]) && dist[nr][nc] == -1)) {
continue;
}
dist[nr][nc] = d + 1;
new_q.emplace_back(nr, nc);
}
}
q = move(new_q);
}
return dist;
};
auto dist = bfs();
const auto& dijkstra = [&](const auto& start, const auto& target) {
priority_queue<pair<int, pair<int, int>>> max_heap;
max_heap.emplace(dist[start.first][start.second], start);
dist[start.first][start.second] = -1;
while (!empty(max_heap)) {
auto [curr, u] = max_heap.top(); max_heap.pop();
if (u == target) {
return curr;
}
for (const auto& [dr, dc] : DIRECTIONS) {
const int nr = u.first + dr, nc = u.second + dc;
if (!(0 <= nr && nr < size(dist) && 0 <= nc && nc < size(dist[0]) && dist[nr][nc] != -1)) {
continue;
}
max_heap.emplace(min(curr, dist[nr][nc]), pair(nr, nc));
dist[nr][nc] = -1;
}
}
return -1;
};
return dijkstra(pair(0, 0), pair(static_cast<int>(size(dist) - 1), static_cast<int>(size(dist[0]) - 1)));
}
};
// Time: O(n^2 * logn)
// Space: O(n^2)
// bfs, binary search
class Solution3 {
public:
int maximumSafenessFactor(vector<vector<int>>& grid) {
static const vector<pair<int, int>> DIRECTIONS = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const auto& bfs = [&]() {
vector<vector<int>> dist(size(grid), vector<int>(size(grid[0]), -1));
vector<pair<int, int>> q;
for (int r = 0; r < size(grid); ++r) {
for (int c = 0; c < size(grid[0]); ++c) {
if (grid[r][c]) {
dist[r][c] = 0;
q.emplace_back(r, c);
}
}
}
int d = 0;
for (int d = 0; !empty(q); ++d) {
vector<pair<int, int>> new_q;
for (const auto& [r, c] : q) {
for (const auto& [dr, dc] : DIRECTIONS) {
const int nr = r + dr, nc = c + dc;
if (!(0 <= nr && nr < size(dist) && 0 <= nc && nc < size(dist[0]) && dist[nr][nc] == -1)) {
continue;
}
dist[nr][nc] = d + 1;
new_q.emplace_back(nr, nc);
}
}
q = move(new_q);
}
return dist;
};
const auto& dist = bfs();
const auto& check = [&](int x) {
vector<vector<int>> lookup(size(dist), vector<int>(size(dist[0])));
vector<pair<int, int>> q = {{0, 0}};
lookup[0][0] = 1;
while (!empty(q)) {
vector<pair<int, int>> new_q;
for (const auto& [r, c] : q) {
for (const auto& [dr, dc] : DIRECTIONS) {
const int nr = r + dr, nc = c + dc;
if (!(0 <= nr && nr < size(dist) && 0 <= nc && nc < size(dist[0]) && dist[nr][nc] >= x && !lookup[nr][nc])) {
continue;
}
lookup[nr][nc] = 1;
new_q.emplace_back(nr, nc);
}
}
q = move(new_q);
}
return lookup.back().back();
};
int left = 0, right = dist[0][0];
while (left <= right) {
const auto& mid = left + (right - left) / 2;
if (!check(mid)) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return right;
}
};