KatInterview

Shortest Evacuation Route Through a Burning Building

Trees & GraphsAuto-graded · 16 test cases
HardAuto-gradedTrees & Graphsbfsgridshortest path

Last updated

Tests BFS shortest-path reasoning on a grid with time-dependent obstacles, requiring candidates to track fire spread and find the minimum escape steps simultaneously.

Shortest Evacuation Route Through a Burning Building

You are given a grid representing a building floor plan. A person starts at a fixed position and must reach any cell on the border of the grid. The building is on fire: fire spreads outward one cell per time step (4-directional), and the person also moves one cell per time step (4-directional).

The person cannot enter a cell that is on fire at the moment they would step into it. Fire spreads and the person moves simultaneously each tick: fire that was at distance d from any fire-source at time 0 will occupy new cells at time d. The person occupying a cell at time t is safe only if the fire does not reach that cell until strictly after time t.

Formally, let fire_time[r][c] be the earliest time step at which fire reaches cell (r, c) (starting from all fire cells simultaneously at time 0). The person may occupy cell (r, c) at time t only if t < fire_time[r][c] (the cell is not yet on fire when the person arrives).

Grid cell values:

  • 0 — open floor (passable)
  • 1 — wall (impassable, fire does not spread through walls)
  • 2 — fire source (fire starts here at time 0)
  • 3 — the person's starting position (treated as open floor for movement and fire-spread purposes)

Goal: Return the minimum number of steps for the person to reach any border cell, or -1 if it is impossible.

A border cell is any cell in row 0, row R-1, column 0, or column C-1, where R and C are the grid dimensions.

If the person starts on a border cell and that cell is safe at time 0, the answer is 0.

Notes:

  • Fire does not spread through walls (1 cells).
  • There is exactly one 3 in the grid.
  • There may be zero or more 2 cells.
  • If a fire source itself is on the border, it is already burning and not a valid exit.

Constraints

  • 1 <= R, C <= 100 (grid dimensions)
  • grid[i][j] ∈ {0, 1, 2, 3}
  • Exactly one cell contains 3.
  • The grid contains at least one cell.
  • There is no constraint on the number of fire sources (may be zero).
  • If the starting cell is on fire at time 0 (a 2 is at that position — this won't occur per the cell definitions, but fire_time of the start is always > 0 since it is a 3 cell treated as open floor, so the person is safe at time 0).

Examples

Example 1

Input: min_evacuation_steps([[3, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 2, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]])

Output: 0

Example 2

Input: min_evacuation_steps([[2, 0, 0], [0, 1, 0], [0, 0, 3]])

Output: 0

Example 3

Input: min_evacuation_steps([[3]])

Output: 0

Hints

Hint 1

Run a multi-source BFS from all fire cells simultaneously to precompute fire_time[r][c] for every cell — this tells you the earliest tick fire arrives at each cell.

Hint 2

Then run a standard BFS from the person's start position, tracking the time step. When considering moving to neighbor (nr, nc) at time t+1, only enqueue it if t+1 < fire_time[nr][nc].

Hint 3

Remember to handle the case of zero fire sources separately: fire_time is effectively infinity everywhere, so it reduces to a plain shortest-path BFS. Also check whether the start is already on the border (answer = 0).