dsa · easy
Flood Fill
GreyOrangeGraphRecursionFoundation
Recolor a 4-connected blob of pixels in a grid. 4-connected means up, down, left, and right — not diagonals.
Arguments
image—m × ngrid of integers;image[r][c]is the color at rowr, columnc(0-based)sr— starting **row** of the fill (0-based)sc— starting **column** of the fill (0-based)color— the new color to paint with
Start at image[sr][sc]. Paint every pixel you can reach by 4-connected steps that currently has the **same** color as that start pixel, changing them to color. Return the modified grid. If the start pixel is already color, return image unchanged.
Example
Start at row 1, column 1 (the centre 1). Paint that blob with 2:
`` image = [[1,1,1], result = [[2,2,2], [1,1,0], [2,2,0], [1,0,1]] [2,0,1]] sr = 1, sc = 1, color = 2 ` The bottom-right 1 stays 1` — it is not 4-connected to the start (diagonal does not count).
Constraints
1 <= m, n <= 50 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [[1,1,1],[1,1,0],[1,0,1]] 1 1 2 Expected: [[2,2,2],[2,2,0],[2,0,1]]