-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2258.cpp
More file actions
78 lines (70 loc) · 1.82 KB
/
2258.cpp
File metadata and controls
78 lines (70 loc) · 1.82 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
76
77
78
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int maximumMinutes(vector<vector<int>>& grid) {
const int m = grid.size(), n = grid[0].size();
int left = 0, right = mn;
bool existPathWhenBreak = false;
while(left < right) {
int mid = left + ((right - left) << 1);
vector<vector<int>> fireGrid = getFireGridAfter(grid, mid);
if(existPath(fireGrid, mid)) {
existPathWhenBreak = true;
left = mid;
}
else {
existPathWhenBreak = false;
right = mid - 1;
}
}
if(existPathWhenBreak) {
if(right == mn) {
return 1e9;
}
return left;
}
else {
return -1;
}
}
};
vector<vector<int>> dirVec = {
{-1, 0},
{1, 0},
{0, 1},
{0, -1}
};
vector<vector<int>> getFireGridAfter(const vector<vector<int>>& grid, int second) {
if(second <= 0) {
return grid;
}
const int m = grid.size(), n = grid[0].size();
vector<vector<int>> res = grid;
queue<pair<int, int>> que;
for(int i = 0; i < m; ++i) {
for(int j = 0; j < n; ++j) {
if(grid[i][j] == 1) {
que.push({i, j});
}
}
}
while(!que.empty()) {
pair<int, int> cur = que.front();
que.pop();
for(auto dir : dirVec) {
int newX = cur.first + dir[0];
int newY = cur.second + dir[1];
if(newX >= 0 && newX < m && newY >= 0 && newY < n) {
if(grid[newX][newY] == 0) {
res[newX][newY] = 1;
}
}
}
}
return getFireGridAfter(res, second - 1);
}
int main() {
Solution solution;
return 0;
}