Source: Flood Fill - LeetCode
题解
class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
# m*n
m,n=len(image),len(image[0])
color = image[sr][sc]
if color == newColor: return image
def dfs(x, y):
if image[x][y] == color:
image[x][y] = newColor
if x>=1:dfs(x-1,y) # ↑
if x+1<m:dfs(x+1,y) # ↓
if y>=1:dfs(x,y-1) # ←
if y+1<n:dfs(x,y+1) # →
dfs(sr,sc)
return image