I am learning Dynamic Programming and am trying to solve the Leet Code Flood Fill problem.
I came up with this solution, but it doesn't recurse correctly through all the possible moves.
Given the input image=[[1,1,1],[1,1,0],[1,0,1]] and sr=1, sc=1, and color=2. I get the incorrect response as [[2,2,1],[2,2,0],[1,0,1]]
def floodFill(
self, image: List[List[int]], sr: int, sc: int, color: int
) -> List[List[int]]:
m = len(image) - 1
n = len(image[0]) - 1
basecolor = image[sr][sc]
self.dfs(image, sr, sc, color, basecolor, m, n)
return image
def dfs(self, image, sr, sc, color, basecolor, m, n):
if sr < 0 or sr > m or sc < 0 or sc > n:
return
if image[sr][sc] == color:
return
if image[sr][sc] != basecolor:
return
image[sr][sc] = color
for direc in [(-1, 0), (0, -1), (1, 0), (0, 1)]:
sr = sr + direc[0]
sc = sc + direc[1]
self.dfs(image, sr, sc, color, basecolor, m, n)
But when I update the list in the for loop, I get the right answer [[2,2,2],[2,2,0],[2,0,1]]
def floodFill(
self, image: List[List[int]], sr: int, sc: int, color: int
) -> List[List[int]]:
m = len(image) - 1
n = len(image[0]) - 1
basecolor = image[sr][sc]
self.dfs(image, sr, sc, color, basecolor, m, n)
return image
def dfs(self, image, sr, sc, color, basecolor, m, n):
if sr < 0 or sr > m or sc < 0 or sc > n:
return
if image[sr][sc] == color:
return
if image[sr][sc] != basecolor:
return
image[sr][sc] = color
for direc in [(sr-1, sc), (sr, sc-1), (sr+1, sc), (sr, sc+1)]:
sr = direc[0]
sc = direc[1]
self.dfs(image, sr, sc, color, basecolor, m, n)
Can someone explain to me why the values of sc or sc in the for loop are causing the error? I think it might have something to do with recursion, but can't figure it out. Or maybe it is something really simple that I have overlooked.
Those loops are not equivalent, they assign
srandscwith different values.Output:
The first mismatch occurs on a second step:
srstays0aftersr = sr + direc[0]since previoussris0and currentdirecis(0,-1).srbecomes1aftersr = direc[0]. Currentdirec(1, 0) was set before the loop has started using an initialsrandscvalues:(sr, sc-1).As the loop continues discrepancies accumulate or cancel out.