Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
01 Matrix
Bookmark
Input
Example 1
Example 2
Corner zeros
Single zero
Custom
grid
=
[[0,0,0],[0,1,0],[0,0,0]]
max dist
0
unvisited
1
0
0
0
0
0
0
0
0
queue
multi-source BFS: distance from every cell to the nearest 0
max dist
0
unvisited
1
0
0
0
0
0
0
0
0
queue
multi-source BFS: distance from every cell to the nearest 0
max dist
0
unvisited
1
0
0
0
0
0
0
0
0
queue
0,1
0,2
1,0
1,2
2,0
2,1
2,2
front
(-1,0)
is off the grid - skip
max dist
1
unvisited
0
0
0
0
0
0
0
0
0
1
queue
0,2
1,0
1,2
2,0
2,1
2,2
1,1
front
dist[1][1] =
0 + 1 = 1
max dist
1
unvisited
0
0
0
0
1
0
0
0
0
0
queue
1,0
1,2
2,0
2,1
2,2
1,1
front
(-1,2)
is off the grid - skip
max dist
1
unvisited
0
0
0
0
1
0
0
0
0
0
queue
1,2
2,0
2,1
2,2
1,1
front
(0,0)
already has a distance - skip
max dist
1
unvisited
0
0
0
0
0
1
0
0
0
0
queue
2,0
2,1
2,2
1,1
front
(1,3)
is off the grid - skip
max dist
1
unvisited
0
0
0
0
0
1
0
0
0
0
queue
2,1
2,2
1,1
front
(2,1)
already has a distance - skip
max dist
1
unvisited
0
0
0
0
0
1
0
0
0
0
queue
2,2
1,1
front
(2,0)
already has a distance - skip
max dist
1
unvisited
0
0
0
0
0
1
0
0
0
0
queue
1,1
front
(2,1)
already has a distance - skip
max dist
1
unvisited
0
0
0
0
0
1
0
0
0
0
queue
return dist
algo
master
.
io
Step:
Multi-source BFS: find each cell's distance to the nearest 0
0 / 50
Input
Example 1
Example 2
Corner zeros
Single zero
Custom
grid
=
[[0,0,0],[0,1,0],[0,0,0]]
0 / 50
algo
master
.
io
Step:
Multi-source BFS: find each cell's distance to the nearest 0