Skip to main content
Back to problems
#2258
Hard Algorithms

Escape the spreading fire

Array Binary Search Breadth-First Search Matrix
37.7% acceptance
Feb 25, 2026
875
42
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).

Solution

Rust
Time O(n * m)
Space O(n * m)
LeetCode
solution.rs
impl Solution {
  pub fn maximum_minutes(grid: Vec<Vec<i32>>) -> i32 {
    use std::collections::VecDeque;
    let m = grid.len();
    let n = grid[0].len();
    const INF: i32 = i32::MAX;
    const DIRS: [(i32, i32); 4] = [(-1,0),(1,0),(0,-1),(0,1)];

    // BFS fire: get fire arrival times
    let mut fire_time = vec![vec![INF; n]; m];
    let mut q: VecDeque<(usize, usize)> = VecDeque::new();
    for r in 0..m {
      for c in 0..n {
        if grid[r][c] == 1 {
          fire_time[r][c] = 0;
          q.push_back((r, c));
        }
      }
    }
    while let Some((r, c)) = q.pop_front() {
      for &(dr, dc) in &DIRS {
        let nr = r as i32 + dr;
        let nc = c as i32 + dc;
        if nr < 0 || nr >= m as i32 || nc < 0 || nc >= n as i32 { continue; }
        let (nr, nc) = (nr as usize, nc as usize);
        if grid[nr][nc] == 2 || fire_time[nr][nc] != INF { continue; }
        fire_time[nr][nc] = fire_time[r][c] + 1;
        q.push_back((nr, nc));
      }
    }

    let can_escape = |wait: i32| -> bool {
      // BFS person starting at (0,0) at time `wait`
      if fire_time[0][0] != INF && wait >= fire_time[0][0] { return false; }
      let mut visited = vec![vec![false; n]; m];
      visited[0][0] = true;
      let mut q: VecDeque<(usize, usize, i32)> = VecDeque::new();
      q.push_back((0, 0, wait));
      while let Some((r, c, t)) = q.pop_front() {
        if r == m-1 && c == n-1 { return true; }
        for &(dr, dc) in &DIRS {
          let nr = r as i32 + dr;
          let nc = c as i32 + dc;
          if nr < 0 || nr >= m as i32 || nc < 0 || nc >= n as i32 { continue; }
          let (nr, nc) = (nr as usize, nc as usize);
          if grid[nr][nc] == 2 || visited[nr][nc] { continue; }
          let nt = t + 1;
          let ft = fire_time[nr][nc];
          let ok = if nr == m-1 && nc == n-1 {
            ft == INF || ft >= nt
          } else {
            ft == INF || ft > nt
          };
          if ok {
            visited[nr][nc] = true;
            q.push_back((nr, nc, nt));
          }
        }
      }
      false
    };

    if !can_escape(0) { return -1; }
    if can_escape(1_000_000_000) { return 1_000_000_000; }
    let mut lo = 0i32;
    let mut hi = 1_000_000_000i32;
    while lo < hi {
      let mid = lo + (hi - lo + 1) / 2;
      if can_escape(mid) { lo = mid; } else { hi = mid - 1; }
    }
    lo
  }
}