Matrices

Problem shape

Beginner
0,0
0,1
0,2
0,3
1,0
1,1
1,2
1,3
2,0
2,1
2,2
2,3
Core idea

Starting from one cell, visit connected cells with the same value and recolor them.

Step 1 / 5
Problem shape
Current lesson

Flood Fill Lab

Matrices Beginner

Flood Fill

Welcome to the Flood Fill algorithm lab. Starting from one cell, visit connected cells with the same value and recolor them. Use it when: Use for image fill, island counting, and connected regions in grids. The time complexity is O(R*C), and the space complexity is O(R*C). The pseudocode is: dfs(r, c) if outside or color differs return set new color dfs four neighbors As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

O(R*C)

Space

O(R*C)

Pseudocode

dfs(r, c)
  if outside or color differs return
  set new color
  dfs four neighbors
Open deeper structure lesson
1.0x