Skip to content

Commit 930063a

Browse files
authored
Create 2258.Escape-the-Spreading-Fire_v4.cpp
1 parent ba323fd commit 930063a

1 file changed

Lines changed: 122 additions & 0 deletions

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
class Solution {
2+
vector<pair<int,int>>dir = {{1,0},{-1,0},{0,1},{0,-1}};
3+
public:
4+
int maximumMinutes(vector<vector<int>>& grid)
5+
{
6+
int m = grid.size();
7+
int n = grid[0].size();
8+
9+
vector<vector<int>>person = bfs(grid, 0);
10+
vector<vector<int>>fire = bfs(grid, 1);
11+
12+
if (person[m-1][n-1]==INT_MAX || person[m-1][n-1] > fire[m-1][n-1])
13+
return -1;
14+
if (fire[m-1][n-1]==INT_MAX)
15+
return 1e9;
16+
17+
vector<vector<int>>visited(m, vector<int>(n));
18+
priority_queue<array<int,3>>pq;
19+
pq.push({fire[m-1][n-1], m-1, n-1});
20+
while (!pq.empty())
21+
{
22+
auto [t, x, y] = pq.top();
23+
pq.pop();
24+
if (visited[x][y]) continue;
25+
visited[x][y] = 1;
26+
if (x==0 && y==0) return t;
27+
28+
for (auto& [dx, dy] : dir)
29+
{
30+
int i = x+dx, j = y+dy;
31+
if (i<0||i>=m||j<0||j>=n) continue;
32+
if (grid[i][j]==2) continue;
33+
if (visited[i][j]) continue;
34+
pq.push({min(t-1,fire[i][j]-1), i,j});
35+
}
36+
}
37+
return -1;
38+
}
39+
40+
bool checkOK(vector<vector<int>>& grid, vector<vector<int>>fire, int D)
41+
{
42+
int m = grid.size();
43+
int n = grid[0].size();
44+
vector<vector<int>>visited(m, vector<int>(n));
45+
46+
queue<pair<int,int>>q;
47+
q.push({0,0});
48+
visited[0][0] = 1;
49+
int step = D;
50+
while (!q.empty())
51+
{
52+
int len = q.size();
53+
while (len--)
54+
{
55+
auto [x,y] = q.front();
56+
q.pop();
57+
for (auto& [dx, dy] : dir)
58+
{
59+
int i = x+dx, j = y+dy;
60+
if (i<0||i>=m||j<0||j>=n) continue;
61+
if (grid[i][j]==2) continue;
62+
if (visited[i][j]) continue;
63+
if ((i!=m-1 || j!=n-1) && step+1 >= fire[i][j]) continue;
64+
q.push({i,j});
65+
visited[i][j] = 1;
66+
if (i==m-1 && j==n-1 && step+1 <= fire[m-1][n-1]) return true;
67+
}
68+
}
69+
step++;
70+
}
71+
return false;
72+
}
73+
74+
vector<vector<int>>bfs(vector<vector<int>>&grid, int type)
75+
{
76+
int m = grid.size();
77+
int n = grid[0].size();
78+
vector<vector<int>>rets(m, vector<int>(n,INT_MAX));
79+
queue<pair<int,int>>q;
80+
81+
if (type==0)
82+
{
83+
q.push({0,0});
84+
rets[0][0] = 0;
85+
}
86+
else
87+
{
88+
for (int i=0; i<m; i++)
89+
for (int j=0; j<n; j++)
90+
if (grid[i][j]==1)
91+
{
92+
q.push({i,j});
93+
rets[i][j] = 0;
94+
}
95+
}
96+
97+
int step = 0;
98+
while (!q.empty())
99+
{
100+
int len = q.size();
101+
while (len--)
102+
{
103+
auto [x,y] = q.front();
104+
q.pop();
105+
106+
for (auto& [dx, dy] : dir)
107+
{
108+
int i = x+dx, j = y+dy;
109+
if (i<0||i>=m||j<0||j>=n) continue;
110+
if (grid[i][j]==2) continue;
111+
if (rets[i][j]!=INT_MAX) continue;
112+
rets[i][j] = step+1;
113+
if (i!=m-1 || j!=n-1)
114+
q.push({i,j});
115+
}
116+
}
117+
step++;
118+
}
119+
120+
return rets;
121+
}
122+
};

0 commit comments

Comments
 (0)