An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).

Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, “flood fill” the image.

To perform a “flood fill”, consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.

At the end, return the modified image.

Example 1:

Input:
image = [[1,1,1],[1,1,0],[1,0,1]]
sr = 1, sc = 1, newColor = 2
Output: [[2,2,2],[2,2,0],[2,0,1]]
Explanation:
From the center of the image (with position (sr, sc) = (1, 1)), all pixels connected
by a path of the same color as the starting pixel are colored with the new color.
Note the bottom corner is not colored 2, because it is not 4-directionally connected
to the starting pixel.


Note:

• The length of image and image[0] will be in the range [1, 50].
• The given starting pixel will satisfy 0 <= sr < image.length and 0 <= sc < image[0].length.
• The value of each color in image[i][j] and newColor will be an integer in [0, 65535].

Solutions

1. DFS-递归

# Time Complexity: O(mn)
# Space Complexity: O(1)
class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
if image[sr][sc] == newColor:
return image
if not image:
return []
r, c = len(image), len(image[0])
self.dfs(image, sr, sc, r, c, image[sr][sc], newColor)
return image

def dfs(self, image, sr, sc, r, c, oldColor, newColor):
if sr < 0 or sr >= r or sc < 0 or sc >= c:
return
if image[sr][sc] != oldColor:
return

image[sr][sc] = newColor

self.dfs(image, sr + 1, sc, r, c, oldColor, newColor)
self.dfs(image, sr - 1, sc, r, c, oldColor, newColor)
self.dfs(image, sr, sc + 1, r, c, oldColor, newColor)
self.dfs(image, sr, sc - 1, r, c, oldColor, newColor)
# Runtime: 104 ms, faster than 7.11% of Python3 online submissions for Flood Fill.
# Memory Usage: 14 MB, less than 5.56% of Python3 online submissions for Flood Fill.


2. DFS-记忆化

# Time Complexity: O(mn)
# Space Complexity: O(n)
class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
color = image[sr][sc]

visited = set()
def dfs(row, col):

if (row, col) in visited:
return

image[row][col] = newColor

if row+1 < len(image) and image[row+1][col] == color:
dfs(row+1, col)

if row-1 >= 0 and image[row-1][col] == color:
dfs(row-1, col)

if col+1 < len(image[0]) and image[row][col+1] == color:
dfs(row, col+1)

if col-1 >= 0 and image[row][col-1] == color:
dfs(row, col-1)
dfs(sr, sc)
return image
# Runtime: 92 ms, faster than 62.89% of Python3 online submissions for Flood Fill.
# Memory Usage: 14 MB, less than 5.56% of Python3 online submissions for Flood Fill.


3. DFS-递归-优化

不需要 Helper，也不需要 Visited，直接将原函数设计成

# Time Complexity: O(mn)
# Space Complexity: O(1)
class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
color = image[sr][sc]
if color == newColor:
return image

image[sr][sc] = newColor

if sr-1 >= 0 and image[sr-1][sc] == color:
image = self.floodFill(image, sr-1, sc, newColor)
if sr+1 < len(image) and image[sr+1][sc] == color:
image = self.floodFill(image, sr+1, sc, newColor)
if sc-1 >= 0 and image[sr][sc-1] == color:
image = self.floodFill(image, sr, sc-1, newColor)
if sc+1 < len(image[0]) and image[sr][sc+1] == color:
image = self.floodFill(image, sr, sc+1, newColor)

return image
# Runtime: 88 ms, faster than 86.98% of Python3 online submissions for Flood Fill.
# Memory Usage: 13.8 MB, less than 5.56% of Python3 online submissions for Flood Fill.