[LeetCode] Escape the Spreading Fire

2258. Escape the Spreading Fire

You are given a 0-indexed 2D integer array grid of size m x n which represents a field. Each cell has one of three values:

  • 0 represents grass,
  • 1 represents fire,
  • 2 represents a wall that you and fire cannot pass through.

You are situated in the top-left cell, (0, 0), and you want to travel to the safehouse at the bottom-right cell, (m - 1, n - 1). Every minute, you may move to an adjacent grass cell. After your move, every fire cell will spread to all adjacent cells that are not walls.

Return the maximum number of minutes that you can stay in your initial position before moving while still safely reaching the safehouse. If this is impossible, return -1. If you can always reach the safehouse regardless of the minutes stayed, return 109.

Note that even if the fire spreads to the safehouse immediately after you have reached it, it will be counted as safely reaching the safehouse.

A cell is adjacent to another cell if the former is directly north, east, south, or west of the latter (i.e., their sides are touching).

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

class Solution {
bool search(vector<vector<int>> grid, vector<pair<int,int>>& fire, int stay) {
int dy[4]{-1,0,1,0}, dx[4]{0,1,0,-1};
int n = grid.size(), m = grid[0].size();
queue<pair<int,int>> f;
queue<pair<int,int>> me;
for(auto p : fire)
f.push(p);
me.push({0,0});
for(int i = 0; i <= stay and !f.empty(); i++) {
int sz = f.size();
while(sz--) {
auto [y, x] = f.front(); f.pop();
for(int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if(0 <= ny and ny < n and 0 <= nx and nx < m and grid[ny][nx] == 0) {
grid[ny][nx] = 1;
f.push({ny,nx});
}
}
}
}

if(grid[0][0] != 0) return false;


while(!me.empty() and grid[n-1][m-1] != 0) {
int sz = f.size();
while(sz--) {
auto [y, x] = f.front(); f.pop();
for(int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if(0 <= ny and ny < n and 0 <= nx and nx < m and grid[ny][nx] <= 0) {
grid[ny][nx] = 1;
f.push({ny,nx});
}
}
}
sz = me.size();
while(sz--) {
auto [y, x] = me.front(); me.pop();
for(int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if(0 <= ny and ny < n and 0 <= nx and nx < m and grid[ny][nx] == 0) {
grid[ny][nx] = -1;
me.push({ny,nx});
}
}
}
}
return grid[n-1][m-1] == -1;
}
public:
int maximumMinutes(vector<vector<int>>& grid) {
int l = 0, r = 1e9, n = grid.size(), m = grid[0].size();
vector<pair<int,int>> fire;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(grid[i][j] == 1)
fire.push_back({i,j});
}
}
int res = -1;
while(l <= r) {
int m = l + (r - l) / 2;
bool p = search(grid, fire, m);
if(p) res = max(res, m);

if(p) l = m + 1;
else r = m -1;
}

return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/01/PS/LeetCode/escape-the-spreading-fire/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.