Flood Fill
You are given an image represented by an m x n grid of integers image, where image[i][j] represents the pixel value of the image. You are also given three integers sr, sc, and color. Your task is to perform a flood fill on the image starting from the pixel image[sr][sc].
To perform a flood fill:
- Begin with the starting pixel and change its color to
color. - Perform the same process for each pixel that is directly adjacent (pixels that share a side with the original pixel, either horizontally or vertically) and shares the same color as the starting pixel.
- Keep repeating this process by checking neighboring pixels of the updated pixels and modifying their color if it matches the original color of the starting pixel.
- The process stops when there are no more adjacent pixels of the original color to update.
Return the modified image after performing the flood fill.
Example 1
1 1 1 1 1 0 1 0 1
Input
image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, color = 2Output
[[2,2,2],[2,2,0],[2,0,1]]From position
(sr, sc) = (1, 1), all horizontally or vertically connected pixels with the same original color are changed to 2, while the disconnected bottom-right pixel remains unchanged.Example 2
0 0 0 0 0 0
Input
image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0Output
[[0,0,0],[0,0,0]]The starting pixel is already colored with
0, which is the same as the target color, so no changes are made.Constraints
- m == image.length
- n == image[i].length
- 1 <= m, n <= 50
- 0 <= image[i][j], color < 2^16
- 0 <= sr < m
- 0 <= sc < n