Flood Fill

easy · graph, dfs, bfs, grid

Flood Fill

An image is represented by an m x n grid of integers. Starting from pixel (sr, sc), replace all pixels connected 4-directionally that have the same color as the starting pixel with newColor. Return the modified image.

Function signature

func FloodFill(image [][]int, sr, sc, newColor int) [][]int

Example

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]]

Constraints

  • 1 <= m, n <= 200
  • 0 <= image[i][j] <= 10
  • 0 <= newColor <= 10

Notes

  • Use DFS/BFS and avoid infinite loops when newColor equals the original color.
Run tests to see results
No issues detected