-
Notifications
You must be signed in to change notification settings - Fork 3
/
2258.cpp
81 lines (77 loc) · 2.9 KB
/
2258.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
class Solution {
public:
int M;
int N;
vector<pair<int, int>> fireInit;
vector<pair<int, int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
bool reachable(vector<vector<int>>& grid, int time) {
// can reach bottom right with time delay
vector<vector<int>> gridCopy = grid;
queue<pair<int, pair<int, int>>> q;
set<pair<int, int>> st;
if (time == 0) {
q.push({4, {0, 0}});
gridCopy[0][0] = 3;
}
for (auto fire : fireInit) {
q.push({3, {fire.first, fire.second}});
gridCopy[fire.first][fire.second] = 1;
}
int level = 0;
while (!q.empty() || level < time) {
int k = q.size();
if (level + 1 <= time && gridCopy[0][0] == 1) return false;
if (level + 1 == time) {
q.push({4, {0, 0}});
gridCopy[0][0] = 3;
}
for (int i = 0; i < k; ++i) {
auto [obj, position] = q.front();
auto [x, y] = position;
q.pop();
if (obj == 4 && st.find({x, y}) != st.end()) continue;
for (auto& direction : directions) {
int nextX = x + direction.first;
int nextY = y + direction.second;
if (nextX < 0 || nextX >= M || nextY < 0 || nextY >= N) continue;
if (nextX == M - 1 && nextY == N - 1 && obj == 4 && gridCopy[nextX][nextY] == 0) return true;
if (gridCopy[nextX][nextY] == 2) continue;
// fire
if (obj == 3) {
if (gridCopy[nextX][nextY] == 1) continue;
if (gridCopy[nextX][nextY] == 3) st.insert({nextX, nextY});
q.push({obj, {nextX, nextY}});
gridCopy[nextX][nextY] = 1;
}
// person
else if (obj == 4) {
if (gridCopy[nextX][nextY] == 3) continue;
if (gridCopy[nextX][nextY] == 1) continue;
q.push({obj, {nextX, nextY}});
gridCopy[nextX][nextY] = 3;
}
}
}
level++;
}
return false;
}
int maximumMinutes(vector<vector<int>>& grid) {
M = grid.size();
N = grid[0].size();
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
if (grid[i][j] == 1) fireInit.push_back({i, j});
}
}
int left = 0;
int right = M * N;
while (left < right) {
int mid = left + (right - left) / 2;
if (!reachable(grid, mid)) right = mid;
else left = mid + 1;
}
if (left == M * N) return 1000000000;
return left - 1;
}
};