颜色填充
程序员文章站
2022-03-21 11:05:13
...
颜色填充
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
row = len(image)
col = len(image[0])
visited = [[False for _ in range(col)]for _ in range(row)]
detection = [[-1,0],[1,0],[0,-1],[0,1]]
def dfs(i,j):
if image[i][j] == oldcolor:
image[i][j] = newColor
for index in detection:
new_x = index[0] + i
new_y = index[1] + j
if 0<= new_x <row and 0<= new_y <col and visited[new_x][new_y]== False and image[new_x][new_y] == oldcolor:
visited[new_x][new_y] = True
dfs(new_x,new_y)
oldcolor = image[sr][sc]
dfs(sr,sc)
return image