Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.
The distance between two adjacent cells is 1.
Example 1:
Input:
0 0 0
0 1 0
0 0 0
Output:
0 0 0
0 1 0
0 0 0
Example 2:
Input:
0 0 0
0 1 0
1 1 1
-1 -1 -1
-1 0 -1
-1 -1
Output:
0 0 0
0 1 0
1 2 1
keywords:
matrix, value only 0 and 1,
statement: the distance two adjacent cell is 1
left, right, up or down , at most 4 adajcent cells
ask: find distance of nearest 0 for each cell
if cell value is 0, no change
if cell value is 1,
0 0 0
0 1 0
0 0 0
0 0 0
0 1 0
1 2 1 <--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.