Multi-Source BFS (Nearest Fire)

VERY EASY · C · 2000ms

Firefighter dispatcher Lamia is mapping a city grid where fire has broken out at multiple points, and needs the distance from EVERY cell to its nearest fire, for evacuation planning. Given an N×M grid where some cells are 'F' (fire) and the rest are '.', print for every cell the minimum distance (in grid steps, 4-directional) to the nearest fire cell.

Input

Line1: N M. Next N lines: grid rows.

Output

N lines, M integers each: distance grid.

Constraints

1 ≤ N,M ≤ 500

Sample Input

3 3
. . .
. F .
. . .

Sample Output

2 1 2
1 0 1
2 1 2
main.c
Loading editor…

Write C, then Run (custom I/O) or Submit (sample tests). ⌘/Ctrl+Enter submits.

Multi-Source BFS (Nearest Fire) · DIU ContestHub