01 Matrix
Given an m x n binary matrix, replace each cell with its distance to the nearest cell containing 0, where distance is the number of single steps up, down, left, or right. Cells that are already 0 have distance 0.
Open official problem prompt ↗Label every cell with how many steps it takes to reach the closest 0.
Imagine every 0 cell lighting up at the same instant and fire spreading outward one square per second. The second at which a cell catches fire is its distance to the nearest ignition point.
- Input
- mat = [[0,0,0],[0,1,0],[1,1,1]]
- Output
- [[0,0,0],[0,1,0],[1,2,1]]
- Why
- The center 1 is one step from a 0; the bottom-middle 1 is two steps from the nearest 0, while the bottom corners are one step away.
m == mat.lengthn == mat[i].length1 <= m, n <= 10^41 <= m * n <= 10^4mat[i][j] is either 0 or 1There is at least one 0 in the matrix