Flood Fill
Problem statement
You are given an integer matrix image, a starting cell (sr, sc), and a replacement value color.
Starting from (sr, sc), replace the value of every cell in the same four-directionally connected component that has the starting cell's original value. Cells are adjacent only when they share an edge.
Return the resulting matrix.
Function
floodFillImage(image: int[][], sr: int, sc: int, color: int) → int[][]Examples
Example 1
image = [[1,1,1],[1,1,0],[1,0,1]]sr = 1sc = 1color = 2return = [[2,2,2],[2,2,0],[2,0,1]]Every 1 connected to (1, 1) through shared edges is recolored. The bottom-right 1 is connected only diagonally, so it remains unchanged.
Example 2
image = [[0,0,0],[0,0,0]]sr = 0sc = 0color = 0return = [[0,0,0],[0,0,0]]The replacement value already equals the component's value, so the matrix does not change.
Example 3
image = [[3,3,4],[3,4,4]]sr = 0sc = 2color = 7return = [[3,3,7],[3,7,7]]The three edge-connected cells containing 4 are recolored to 7.
Constraints
1 <= image.length, image[r].length <= 50.- All rows have the same length.
0 <= image[r][c], color <= 65535.0 <= sr < image.lengthand0 <= sc < image[0].length.