Published: Sep 27, 2022
This is a simple matrix search problem. Either of breadth-first or depth-first search works. Since the cell color is changed when visited, it doesn’t need to maintain visited cells. Just visit and change color, then we will get the answer.
An image is represented by an
m x ninteger grid
image[i][j]represents the pixel value of the image. You are also given three integers
color. You should perform a flood fill on the image starting from the pixel
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), and so on. Replace the color of all of the aforementioned pixels with color.
Return the modified image after performing the flood fill.
m == image.length
n == image[i].length
1 <= m, n <= 50
0 <= image[i][j], color < 2**16
0 <= sr < m
0 <= sc < n
Example 1 Input: image = [[1,1,1],[1,1,0],[1,0,1]], sr = 1, sc = 1, color = 2 Output: [[2,2,2],[2,2,0],[2,0,1]]
Example 2 Input: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0 Output: [[0,0,0],[0,0,0]]
The solution here uses the breadth-first search (BFS). BFS might run slow, but can avoid a stack level too deep error. Before starting BFS, it check the color at the starting cell. If the starting cell has the same color as the new color, there’s nothing to do. The BFS is nothing special. It checks 4 neighbors, updates color and append the cell to the BFS queue.
class FloodFill: def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]: m, n = len(image), len(image) queue, prev = [(sr, sc)], image[sr][sc] if color == prev: return image while queue: row, col = queue.pop(0) image[row][col] = color for r, c in [(row - 1, col), (row, col - 1), (row, col + 1), (row + 1, col)]: if 0 <= r < m and 0 <= c < n and image[r][c] == prev: queue.append((r, c)) return image
O(m * n)