颜色填充

颜色填充

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

猜你喜欢

转载自blog.csdn.net/aaaqqq1234/article/details/108300384