mediumGraphsFacebookGoogle
Firewatch Distance
A fire marshal annotates every empty room with the shortest Manhattan distance to the nearest emergency exit (gate). Walls block paths.
Each cell is 0 (gate), -1 (wall), or INT_MAX (empty room). Fill empty rooms with the distance to the nearest gate.
Choose approach
2147483647
-1
0
2147483647
2147483647
2147483647
2147483647
-1
2147483647
-1
2147483647
-1
0
-1
2147483647
2147483647
1/10
Narrationstep 1 of 10
Seed BFS from every gate. Walls stay out.
Variables
gates=2
How to think about it
- Multi-source BFS = single BFS with multiple roots.
- First touch is cheapest in unweighted graphs.
- O(R·C) because each cell is enqueued once.
Edge cases to catch in interview
- Isolated room -> stays INT_MAX.
- No gates -> grid unchanged.
Examples
Example 1
[[1,0]]{"grid":[[2147483647,0]]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed