Escape the Spreading Fire
master · 1770 · lc hard +32 · verified · 37.7% accepted · 876 likes · top 16%
Description
You are given a 0-indexed 2D integer array grid of size m x n where:
- 0 represents open grass,
- 1 represents fire,
- 2 represents an impassable wall.
You begin at (0, 0) and must reach the safehouse at (m - 1, n - 1). Each minute you step to an adjacent grass cell, then every fire spreads to its non-wall neighbors.
Return the maximum number of minutes you may wait at the start before moving and still safely reach the safehouse. Return -1 if safe arrival is impossible. Return 109 if you can always escape regardless of wait time.
Arriving at the safehouse on the same minute as the fire reaches it still counts as safe.
Adjacent cells share a side.
Example 1:
Example 2:
Example 3:
Code