#C4290. Distance to Nearest Zero in Matrix
Distance to Nearest Zero in Matrix
Distance to Nearest Zero in Matrix
You are given an m x n matrix consisting only of 0s and 1s. For each cell, your task is to compute the distance to the nearest 0 using a breadth-first search approach. The distance between two adjacent cells is 1, and it is defined by the Manhattan distance, i.e. \( |x_1 - x_2| + |y_1 - y_2| \). If a cell has no path to a 0, output INF
for that cell.
The input is read from stdin and the output should be printed to stdout. Please ensure that your solution handles multiple test cases correctly and adheres to the format described.
inputFormat
The first line contains two integers, m and n, representing the number of rows and columns of the matrix respectively. The following m lines each contain n integers (either 0 or 1) separated by spaces.
outputFormat
Output exactly m lines. Each line contains n space-separated values. For each cell, if a 0 is reachable, print the minimum distance (an integer); otherwise, print INF
(without quotes).
3 3
0 0 0
0 1 0
0 0 0
0 0 0
0 1 0
0 0 0
</p>