You are given a 2D grid image of size m × n, where each cell contains an integer color value. You are also given a starting cell (sr, sc) and an integer newColor.
Perform a flood fill starting from (sr, sc):
oldColor = image[sr][sc]
.
(sr, sc)
and every cell
4-directionally connected
(up, down, left, right) to
(sr, sc)
that has color
oldColor
to
newColor
.
image
.
If oldColor == newColor, the grid should remain unchanged.
Write the solution in three ways:
image: int[m][n]
,
sr: int
,
sc: int
,
newColor: int
image
after flood fill.
1 ≤ m, n ≤ 50
0 ≤ sr < m
,
0 ≤ sc < n
Given:
image = [[1,1,1],[1,1,0],[1,0,1]]
,
sr = 1
,
sc = 1
,
newColor = 2
Output:
[[2,2,2],[2,2,0],[2,0,1]]