📘
Flood Fill
EasyGraphs & Search
Given a 2D image grid, a starting pixel (sr, sc), and a new color, perform a flood fill — change the starting pixel and all connected same-colored pixels to the new color.
Output the resulting grid, each row on a new line, values space-separated.
Example:
Input:
1,1,1 1,1,0 1,0,1 1 1 2
Output:
2 2 2 2 2 0 2 0 1
Reasoning:
- The input image grid is: 1 1 1 1 1 0 1 0 1 with a starting pixel at position (1, 1) and a new color of 2.
- We start the flood fill from the pixel at position (1, 1) with a value of 1 and replace it with the new color 2.
- All adjacent pixels with the same color (1) are also replaced with the new color 2, which includes the pixels at positions (0, 0), (0, 1), (0, 2), (1, 0), and (2, 0), (2, 1).
- The resulting grid after the flood fill operation is: 2 2 2 2 2 0 2 0 1
Constraints:
- 1 <= rows, cols <= 50
- 0 <= image[i][j], color <= 65535
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.