All problems
Flood Fill Region Replacement
Problem
Flood fill a 2D image starting from (sr, sc) using 4-connectivity.
Replace every pixel connected to the start whose value equals the starting value with new_color. Return the resulting image; if the start value already equals new_color, return an unchanged copy.
Hint
Use a stack of pixel coordinates; check bounds and equality before pushing.
Test cases
case 1
in: [[[1,1,0],[1,0,0],[0,0,1]],0,0,2]
expected: [[2,2,0],[2,0,0],[0,0,1]]
case 2
in: [[[1,1]],0,0,1]
expected: [[1,1]]
case 3
in: [[[1,0],[0,1]],0,0,9]
expected: [[9,0],[0,1]]
case 4
in: [[[1,1,1],[1,0,0],[1,0,0]],1,2,5]
expected: [[1,1,1],[1,5,5],[1,5,5]]
Starter codePython
def flood_fill(image, sr, sc, new_color):
# Your code here
pass4 test cases · run your code against them in the editor