Minutes to Burn
grid is a list of strings, one per row, in which . is dry grass, F is a cell that is already on fire and # is rock. Every minute, each burning cell sets fire to the grass cells that share a side with it (up, down, left, right), all at the same time. Rock never burns and fire cannot pass through it. Return the number of minutes until no grass is left. If there is no grass at the start, return 0. If some grass can never catch fire, return -1.
Example 1
The fire starts in a corner of a 3 by 3 field; the opposite corner is 4 steps away, so it catches fire in minute 4.
Example 2
The rock column blocks the direct way, so the fire has to go round the bottom to reach the top-right cell, 7 steps away.
Constraints
1 <= rows, cols <= 10grid[r]has only the characters.,Fand#
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Grids problems
- Paths Around RocksMedium
- Ramp DropMedium
- Settle the SandpileMedium
- Slide and MergeMedium
- Snake GameMedium
- Spiral FillMedium
- Spiral OrderMedium
- Spiral Out From a CellMedium