classSolution: defmaximumMinutes(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0])
defcheck(t: int) -> bool: f = [(i, j) for i, row inenumerate(grid) for j, v inenumerate(row) if v == 1] fire = set(f) defspread_fire(): nonlocal f tmp = f f = [] for i, j in tmp: for x, y in (i, j - 1), (i, j + 1), (i - 1, j), (i + 1, j): if0 <= x < m and0 <= y < n and grid[x][y] == 0and (x, y) notin fire: fire.add((x, y)) f.append((x, y)) while t and f: spread_fire() # 蔓延至多 t 分钟的火势 t -= 1 if (0, 0) in fire: # 起点着火,寄 returnTrue
q = [(0, 0)] vis = set(q) while q: tmp = q q = [] for i, j in tmp: if (i, j) notin fire: for x, y in (i, j - 1), (i, j + 1), (i - 1, j), (i + 1, j): if0 <= x < m and0 <= y < n and grid[x][y] == 0and (x, y) notin fire and (x, y) notin vis: if x == m - 1and y == n - 1: # 我们安全了…暂时。 returnFalse vis.add((x, y)) q.append((x, y)) spread_fire() # 蔓延 1 分钟的火势 returnTrue# 寄
ans = bisect_left(range(m * n + 1), True, key=check) - 1 return ans if ans < m * n else10 ** 9
publicintmaximumMinutes(int[][] grid) { intm= grid.length, n = grid[0].length; intleft= -1, right = m * n; while (left < right) { varmid= (left + right + 1) / 2; if (check(grid, mid)) left = mid; else right = mid - 1; } return left < m * n ? left : (int) 1e9; }
booleancheck(int[][] grid, int t) { intm= grid.length, n = grid[0].length; varfire=newboolean[m][n]; varf=newArrayList<int[]>(); for (vari=0; i < m; i++) for (varj=0; j < n; j++) if (grid[i][j] == 1) { fire[i][j] = true; f.add(newint[]{i, j}); } while (t-- > 0 && f.size() > 0) f = spreadFire(grid, fire, f); // 蔓延至多 t 分钟的火势 if (fire[0][0]) returnfalse; // 起点着火,寄
varvis=newboolean[m][n]; vis[0][0] = true; varq=newArrayList<int[]>(); q.add(newint[]{0, 0}); while (q.size() > 0) { vartmp= q; q = newArrayList<>(); for (var p : tmp) if (!fire[p[0]][p[1]]) for (var d : dirs) { intx= p[0] + d[0], y = p[1] + d[1]; if (0 <= x && x < m && 0 <= y && y < n && !fire[x][y] && !vis[x][y] && grid[x][y] == 0) { if (x == m - 1 && y == n - 1) returntrue; // 我们安全了…暂时。 vis[x][y] = true; q.add(newint[]{x, y}); } } f = spreadFire(grid, fire, f); // 蔓延 1 分钟的火势 } returnfalse; // 寄 }
ArrayList<int[]> spreadFire(int[][] grid, boolean[][] fire, ArrayList<int[]> f) { intm= grid.length, n = grid[0].length; vartmp= f; f = newArrayList<>(); for (var p : tmp) for (var d : dirs) { intx= p[0] + d[0], y = p[1] + d[1]; if (0 <= x && x < m && 0 <= y && y < n && !fire[x][y] && grid[x][y] == 0) { fire[x][y] = true; f.add(newint[]{x, y}); } } return f; } }
classSolution { boolcheck(vector<vector<int>> &grid, int t){ int m = grid.size(), n = grid[0].size(); bool fire[m][n]; memset(fire, 0, sizeof(fire)); vector<pair<int, int>> f, q; for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) if (grid[i][j] == 1) { fire[i][j] = true; f.emplace_back(i, j); } auto spread_fire = [&]() { vector<pair<int, int>> nf; for (auto &[i, j] : f) for (auto [dx, dy] : dirs) { int x = i + dx, y = j + dy; if (0 <= x && x < m && 0 <= y && y < n && !fire[x][y] && grid[x][y] == 0) { fire[x][y] = true; nf.emplace_back(x, y); } } f = move(nf); }; while (t-- && !f.empty()) spread_fire(); // 蔓延至多 t 分钟的火势 if (fire[0][0]) returnfalse; // 起点着火,寄
bool vis[m][n]; memset(vis, 0, sizeof(vis)); vis[0][0] = true; q.emplace_back(0, 0); while (!q.empty()) { vector<pair<int, int>> nq; for (auto &[i, j] : q) if (!fire[i][j]) for (auto [dx, dy] : dirs) { int x = i + dx, y = j + dy; if (0 <= x && x < m && 0 <= y && y < n && !fire[x][y] && !vis[x][y] && grid[x][y] == 0) { if (x == m - 1 && y == n - 1) returntrue; // 我们安全了…暂时。 vis[x][y] = true; nq.emplace_back(x, y); } } q = move(nq); spread_fire(); // 蔓延 1 分钟的火势 } returnfalse; // 寄 }
public: intmaximumMinutes(vector<vector<int>> &grid){ int m = grid.size(), n = grid[0].size(); int left = -1, right = m * n; while (left < right) { int mid = (left + right + 1) / 2; if (check(grid, mid)) left = mid; else right = mid - 1; } return left < m * n ? left : 1e9; } };
type pair struct{ x, y int } var dirs = []pair{ {-1, 0}, {1, 0}, {0, -1}, {0, 1} }
funcmaximumMinutes(grid [][]int)int { m, n := len(grid), len(grid[0]) ans := sort.Search(m*n+1, func(t int)bool { fire := make([][]bool, m) for i := range fire { fire[i] = make([]bool, n) } f := []pair{} for i, row := range grid { for j, v := range row { if v == 1 { fire[i][j] = true f = append(f, pair{i, j}) } } } spreadFire := func() { tmp := f f = nil for _, p := range tmp { for _, d := range dirs { if x, y := p.x+d.x, p.y+d.y; 0 <= x && x < m && 0 <= y && y < n && !fire[x][y] && grid[x][y] == 0 { fire[x][y] = true f = append(f, pair{x, y}) } } } } for ; t > 0 && len(f) > 0; t-- { spreadFire() // 蔓延至多 t 分钟的火势 } if fire[0][0] { // 起点着火,寄 returntrue }
vis := make([][]bool, m) for i := range vis { vis[i] = make([]bool, n) } vis[0][0] = true q := []pair{ {} } forlen(q) > 0 { tmp := q q = nil for _, p := range tmp { if !fire[p.x][p.y] { for _, d := range dirs { if x, y := p.x+d.x, p.y+d.y; 0 <= x && x < m && 0 <= y && y < n && !vis[x][y] && !fire[x][y] && grid[x][y] == 0 { if x == m-1 && y == n-1 { // 我们安全了…暂时。 returnfalse } vis[x][y] = true q = append(q, pair{x, y}) } } } } spreadFire() // 蔓延 1 分钟的火势 } returntrue// 寄 }) - 1 if ans < m*n { return ans } return1e9 }
classSolution: defmaximumMinutes(self, grid: List[List[int]]) -> int: m, n = len(grid), len(grid[0]) defbfs(q: List[Tuple[int, int]]) -> (int, int, int): time = [[-1] * n for _ inrange(m)] for i, j in q: time[i][j] = 0 t = 1 while q: tmp = q q = [] for i, j in tmp: for x, y in (i, j - 1), (i, j + 1), (i - 1, j), (i + 1, j): if0 <= x < m and0 <= y < n and grid[x][y] == 0and time[x][y] < 0: time[x][y] = t q.append((x, y)) t += 1 return time[-1][-1], time[-1][-2], time[-2][-1]
man_to_house_time, m1, m2 = bfs([(0, 0)]) if man_to_house_time < 0: return -1# 人无法到终点 fire_to_house_time, f1, f2 = bfs([(i, j) for i, row inenumerate(grid) for j, v inenumerate(row) if v == 1]) if fire_to_house_time < 0: return10 ** 9# 火无法到终点 ans = fire_to_house_time - man_to_house_time if ans < 0: return -1# 火比人先到终点 if m1 < 0or m2 < 0or f1 - m1 == f2 - m2 == ans: return ans - 1# 火只会跟在人的后面,在到达终点前,人和火不能重合 return ans # 人和火可以同时到终点
vector<pair<int, int>> fires; for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) if (grid[i][j] == 1) fires.emplace_back(i, j); auto [fire_to_house_time, f1, f2] = bfs(fires); if (fire_to_house_time < 0) return1e9; // 火无法到终点
int ans = fire_to_house_time - man_to_house_time; if (ans < 0) return-1; // 火比人先到终点 if (m1 < 0 || m2 < 0 || f1 - m1 == ans && f2 - m2 == ans) return ans - 1; // 火只会跟在人的后面,在到达终点前,人和火不能重合 return ans;// 人和火可以同时到终点 } };
type pair struct{ x, y int } var dirs = []pair{ {-1, 0}, {1, 0}, {0, -1}, {0, 1} }
funcmaximumMinutes(grid [][]int)int { m, n := len(grid), len(grid[0]) bfs := func(q []pair) (int, int, int) { time := make([][]int, m) for i := range time { time[i] = make([]int, n) for j := range time[i] { time[i][j] = -1 } } for _, p := range q { time[p.x][p.y] = 0 } for t := 1; len(q) > 0; t++ { tmp := q q = nil for _, p := range tmp { for _, d := range dirs { if x, y := p.x+d.x, p.y+d.y; 0 <= x && x < m && 0 <= y && y < n && grid[x][y] == 0 && time[x][y] < 0 { time[x][y] = t q = append(q, pair{x, y}) } } } } return time[m-1][n-1], time[m-1][n-2], time[m-2][n-1] }
fires := []pair{} for i, row := range grid { for j, v := range row { if v == 1 { fires = append(fires, pair{i, j}) } } } fireToHouseTime, f1, f2 := bfs(fires) if fireToHouseTime < 0 { return1e9// 火无法到终点 }
ans := fireToHouseTime - manToHouseTime if ans < 0 { return-1// 火比人先到终点 } if m1 < 0 || m2 < 0 || f1-m1 == ans && f2-m2 == ans { return ans - 1// 火只会跟在人的后面,在到达终点前,人和火不能重合 } return ans // 人和火可以同时到终点 }