Given an m x n binary matrix mat, return a matrix of the same size where each cell holds the distance to the nearest 0. Two cells sharing an edge are at distance 1.
Given an m x n binary matrix mat, return a matrix of the same size where each cell holds the distance to the nearest 0. Two cells sharing an edge are at distance 1.
Example 1:
Input: {"mat":[[0,0,0],[0,1,0],[0,0,0]]}
Output: [[0,0,0],[0,1,0],[0,0,0]]
Constraints:
m=mat.length, n=mat[i].length
1≤m,n≤104
1≤m⋅n≤104
mat[i][j] is either 0 or 1.
There is at least one 0 in mat.
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"mat":[[0,0,0],[0,1,0],[0,0,0]]}
Make a 3 x 3 distance grid filled with -1, meaning "not reached yet".
no wave yet
reached 0 / 9
0
0
0
0
?
0
0
0
0
queue (0)
empty
0 (source)
leaving queue
just reached
already set
Arrows show which way the wave travelled into a cell.