Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Flood Fill
Bookmark
Input
Example 1
Same Color
No Change
Cross Pattern
Custom
image
=
[[1,1,1],[1,1,0],[1,0,1]]
,
sr
=
1
,
sc
=
1
,
color
=
2
fill
1
→
2
painted
0
0
1
2
0
1
2
1
1
1
1
1
0
1
0
1
start
CALL STACK
floodFill
DFS repaints every 4-connected pixel of the start colour
fill
1
→
2
painted
0
0
1
2
0
1
2
1
1
1
1
1
0
1
0
1
start
CALL STACK
floodFill
DFS repaints every 4-connected pixel of the start colour
fill
1
→
2
painted
2
0
1
2
0
1
2
1
2
1
1
2
0
1
0
1
CALL STACK
dfs(0, 1)
dfs(1, 1)
floodFill
explore
up
from (0, 1)
fill
1
→
2
painted
3
0
1
2
0
1
2
2
2
1
1
2
0
1
0
1
CALL STACK
dfs(0, 0)
dfs(0, 1)
dfs(1, 1)
floodFill
explore
up
from (0, 0)
fill
1
→
2
painted
4
0
1
2
0
1
2
2
2
1
2
2
0
1
0
1
CALL STACK
dfs(1, 0)
dfs(0, 0)
dfs(0, 1)
dfs(1, 1)
floodFill
explore
down
from (1, 0)
fill
1
→
2
painted
5
0
1
2
0
1
2
2
2
1
2
2
0
2
0
1
CALL STACK
dfs(2, 0)
dfs(1, 0)
dfs(0, 0)
dfs(0, 1)
dfs(1, 1)
floodFill
explore
left
from (2, 0)
fill
1
→
2
painted
5
0
1
2
0
1
2
2
2
1
2
2
0
2
0
1
CALL STACK
dfs(1, 0)
dfs(0, 0)
dfs(0, 1)
dfs(1, 1)
floodFill
explore
right
from (1, 0)
fill
1
→
2
painted
5
0
1
2
0
1
2
2
2
1
2
2
0
2
0
1
CALL STACK
dfs(0, 0)
dfs(0, 1)
dfs(1, 1)
floodFill
all neighbours of
(0, 0)
done — backtrack
fill
1
→
2
painted
6
0
1
2
0
1
2
2
2
2
2
2
0
2
0
1
CALL STACK
dfs(1, 2)
dfs(0, 2)
dfs(0, 1)
dfs(1, 1)
floodFill
(1, 2)
= 0 ≠ 1 — skip
fill
1
→
2
painted
6
0
1
2
0
1
2
2
2
2
2
2
0
2
0
1
CALL STACK
dfs(1, 1)
floodFill
explore
down
from (1, 1)
fill
1
→
2
painted
6
0
1
2
0
1
2
2
2
2
2
2
0
2
0
1
CALL STACK
floodFill
flood fill complete · 6 pixels painted
algo
master
.
io
Step:
Start: Flood fill from (1, 1) with color 2
0 / 63
Input
Example 1
Same Color
No Change
Cross Pattern
Custom
image
=
[[1,1,1],[1,1,0],[1,0,1]]
,
sr
=
1
,
sc
=
1
,
color
=
2
0 / 63
algo
master
.
io
Step:
Start: Flood fill from (1, 1) with color 2