Skip to content

Practice Escape the Spreading Fire

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#2258
LeetCode Problem

Escape the Spreading Fire

Problem Statement

grid uses 0 for grass, 1 for initial fire, and 2 for impassable walls. Wait at (0,0), then move one side-adjacent grass cell per minute toward the bottom-right safehouse. Fire spreads one cell in all four directions through nonwalls every minute, including each minute of the initial wait. After movement, a cell reached by fire cannot be occupied unless it is the safehouse just reached that minute. Reaching the safehouse just before fire spreads there is allowed. Return the maximum safe initial wait, -1 if escape is impossible, or 10^9 if waiting is unbounded.

Source: Escape the Spreading Fire on LeetCode. Use the original task for its coding environment and submissions.

Examples

# 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

# Example 2

Input
grid = [[0,0,0,0],[0,1,2,0],[0,2,0,0]]
Output
-1

# Example 3

Input
grid = [[0,0,0],[2,2,0],[1,2,0]]
Output
1000000000

Constraints

m == grid.length
n == grid[i].length
2 <= m, n <= 300
4 <= m * n <= 2 * 10^4
grid[i][j] is either 0, 1, or 2.
grid[0][0] == grid[m - 1][n - 1] == 0

Related LeetCode Problems

Practice more problems similar to Escape the Spreading Fire.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.