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).
Example 1:
Input: grid = [[0,2,0,0,0,0,0],[0,0,0,2,2,1,0],[0,2,0,0,1,2,0],[0,0,2,2,2,0,2],[0,0,0,0,0,0,0]] Output: 3 Explanation: The figure above shows the scenario where you stay in the initial position for 3 minutes. You will still be able to safely reach the safehouse. Staying for more than 3 minutes will not allow you to safely reach the safehouse.
Example 2:
Input: grid = [[0,0,0,0],[0,1,2,0],[0,2,0,0]] Output: -1 Explanation: The figure above shows the scenario where you immediately move towards the safehouse. Fire will spread to any cell you move towards and it is impossible to safely reach the safehouse. Thus, -1 is returned.
Example 3:
Input: grid = [[0,0,0],[2,2,0],[1,2,0]] Output: 1000000000 Explanation: The figure above shows the initial grid. Notice that the fire is contained by walls and you will always be able to safely reach the safehouse. Thus, 109 is returned.
Constraints:
m == grid.lengthn == grid[i].length2 <= m, n <= 3004 <= m * n <= 2 * 104grid[i][j] is either 0, 1, or 2.grid[0][0] == grid[m - 1][n - 1] == 0#2258 Escape the Spreading Fire is a challenging grid simulation problem that combines multi-source Breadth-First Search (BFS) with binary search. The key insight is that the fire spreads each minute, so we must determine the maximum time a person can wait before starting to move while still reaching the safe house.
First, run a multi-source BFS from all fire cells to compute the earliest time the fire reaches every cell in the matrix. This produces a time grid that represents the fire spread schedule. Next, apply binary search on the waiting time. For each candidate waiting time, simulate the person's movement using BFS and ensure that the person never reaches a cell at or after the time the fire arrives.
If the destination can still be reached safely, increase the waiting time; otherwise decrease it. This combination of BFS preprocessing and binary search efficiently determines the maximum safe delay.
Time Complexity: O(m × n × log(m × n)) due to repeated BFS checks during binary search. Space Complexity: O(m × n) for storing fire arrival times and BFS states.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Multi-source BFS to compute fire spread | O(m × n) | O(m × n) |
| Binary Search with BFS feasibility check | O(m × n × log(m × n)) | O(m × n) |
Greg Hogg
Use these hints if you're stuck. Try solving on your own first.
For some tile (x, y), how can we determine when, if ever, the fire will reach it?
We can use multi-source BFS to find the earliest time the fire will reach each cell.
Then, starting with a given t minutes of staying in the initial position, we can check if there is a safe path to the safehouse using the obtained information about the fire.
We can use binary search to efficiently find the maximum t that allows us to reach the safehouse.
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, variations of grid BFS and simulation problems like this are common in FAANG-style interviews. They test understanding of BFS, matrix traversal, and combining techniques such as binary search with graph exploration.
Binary search helps determine the maximum number of minutes the player can wait before moving. Since the feasibility of reaching the safe house changes monotonically with waiting time, binary search efficiently finds the largest valid delay.
Queues are essential because the problem relies heavily on Breadth-First Search. BFS is used both for spreading the fire across the grid and for simulating the player's movement through the matrix.
The optimal approach combines multi-source BFS and binary search. First compute when the fire reaches each cell using BFS, then binary search the maximum waiting time while checking reachability with another BFS simulation.