Flood Fill is a classic image processing algorithm that simulates the "paint bucket" tool found in graphics programs like MS Paint or Photoshop.

You're given an image represented as a 2D grid where each cell contains a pixel value (color). Starting from a specified pixel at position (sr, sc), you need to "flood" all connected pixels of the same original color with a new color.

The Process:
1. Start at pixel image[sr][sc] and note its original color
2. Change this pixel to the new color
3. Recursively do the same for all adjacent pixels (up, down, left, right) that have the same original color
4. Stop when no more connected pixels have the original color

Think of it like spilling paint on a connected area - it spreads to all touching pixels of the same color!

Input & Output

example_1.py โ€” Basic Flood Fill
$ Input: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, color = 2
โ€บ Output: [[2,2,2],[2,2,0],[2,0,1]]
๐Ÿ’ก Note: Starting from center position (1,1) with original color 1, we flood fill with color 2. All connected pixels with value 1 get changed to 2, but the 0s act as boundaries and remain unchanged.
example_2.py โ€” Same Color Edge Case
$ Input: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0
โ€บ Output: [[0,0,0],[0,0,0]]
๐Ÿ’ก Note: The starting pixel already has the target color (0), so no changes are needed. The algorithm returns early to avoid infinite recursion.
example_3.py โ€” Single Pixel
$ Input: image = [[1]], sr = 0, sc = 0, color = 3
โ€บ Output: [[3]]
๐Ÿ’ก Note: Simple case with just one pixel. The pixel at (0,0) has value 1 and gets changed to 3.

Constraints

  • m == image.length
  • n == image[i].length
  • 1 โ‰ค m, n โ‰ค 50
  • 0 โ‰ค image[i][j], color < 216
  • 0 โ‰ค sr < m
  • 0 โ‰ค sc < n

Visualization

Tap to expand
๐ŸŽจ Digital Paint Bucket ToolBefore: Click on blue areaCLICKAfter: All connected blue โ†’ red๐Ÿ”‘ Algorithm InsightPaint spreads only to connected pixels of the SAME colorDifferent colors act as natural boundaries!๐Ÿ” Real-World Applications:โ€ข Graphics software (Photoshop, GIMP, MS Paint)โ€ข Game development (terrain filling, region selection)โ€ข Medical imaging (organ segmentation)โ€ข Computer vision (connected component analysis)โ€ข Geographic Information Systems (GIS)โ€ข Image processing and analysisโ€ข Pattern recognition systemsโ€ข Digital art and design tools
Understanding the Visualization
1
Click Starting Pixel
User clicks on a blue pixel with red paint selected
2
Paint Spreads
Red paint spreads to all connected blue pixels
3
Boundaries Stop Spread
Yellow pixels act as boundaries - paint doesn't cross them
4
Filling Complete
All connected blue pixels are now red
Key Takeaway
๐ŸŽฏ Key Insight: Flood fill is like a smart paint bucket that only spreads to connected pixels of the same original color, making it perfect for region-based operations in graphics and image processing.
Asked in
Google 42 Amazon 38 Microsoft 35 Meta 28 Adobe 25
52.8K Views
Medium-High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen