Solution: Flood Fill
Explore how to solve the flood fill problem using a backtracking approach with depth-first search. Understand recursive filling of connected pixels in a grid and learn to apply boundary and color checks. This lesson helps you implement an efficient solution that updates all adjacent pixels sharing the original color, refining your grasp of grid-based algorithms and recursion.
We'll cover the following...
Statement
You are given a 2D grid of size m x n, where grid[i][j] represents the pixel value at position (i, j).
Additionally, you are given three integers:
sr: The row index of the starting pixelsc: The column index of the starting pixeltarget: The new color to apply
Your task is to perform a flood fill starting from the pixel at position (sr, sc). Below is the flood fill process:
If the pixel at
(sr, sc)already has the valuetarget, return the original grid without any changes.Otherwise, start at the pixel
grid[sr][sc]and change its color to the given colortarget. ...