#C7633. Minimum Distance to Filled Cells
Minimum Distance to Filled Cells
Minimum Distance to Filled Cells
You are given a grid with R rows and C columns. Each cell in the grid is either filled (represented by the character #
) or empty (represented by the character .
). Your task is to compute, for every cell in the grid, the minimum Manhattan distance to any filled cell.
The Manhattan distance between two cells at positions \((i,j)\) and \((i',j')\) is given by:
[ d((i,j), (i',j')) = |i-i'| + |j-j'| ]
If a cell cannot reach any filled cell (i.e. there are no filled cells in the grid), output INF
for that cell.
Note: Movement is allowed only in four directions: up, down, left, and right.
inputFormat
The first line contains two space-separated integers R
and C
(1 ≤ R, C ≤ 1000), representing the number of rows and columns of the grid respectively.
The following R
lines each contain a string of length C
consisting only of the characters #
and .
, representing the grid.
outputFormat
Output R
lines. Each line should contain C
space-separated tokens. For each cell, output the minimum Manhattan distance to a filled cell. If a cell is not reachable from any filled cell, output INF
(without quotes) for that cell.
1 1
#
0
</p>