-
Notifications
You must be signed in to change notification settings - Fork 3
/
2812.cpp
42 lines (40 loc) · 1.42 KB
/
2812.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
class Solution {
public:
vector<pair<int ,int >> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
int maximumSafenessFactor(vector<vector<int>>& grid) {
int n = grid.size();
queue<pair<int, int>> q;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1) q.push({i, j});
}
}
while (!q.empty()) {
auto [i, j] = q.front();
q.pop();
for (auto& direction : directions) {
int x = i + direction.first;
int y = j + direction.second;
if (x < 0 || x >= n || y < 0 || y >= n) continue;
if (grid[x][y] != 0) continue;
grid[x][y] = grid[i][j] + 1;
q.push({x, y});
}
}
priority_queue<tuple<int, int, int>> pq;
pq.push({grid[0][0], 0, 0});
while (get<1>(pq.top()) < n - 1 || get<2>(pq.top()) < n - 1) {
auto [val, i, j] = pq.top();
pq.pop();
for (auto& direction : directions) {
int x = i + direction.first;
int y = j + direction.second;
if (x < 0 || x >= n || y < 0 || y >= n) continue;
if (grid[x][y] < 0) continue;
pq.push({min(val, grid[x][y]), x, y});
grid[x][y] *= -1;
}
}
return get<0>(pq.top()) - 1;
}
};