Image segmentation often begins by identifying spatially contiguous regions of similar features or colors. The Flood Fill algorithm is a classic graph traversal technique (typically implemented via Depth-First Search or Breadth-First Search) used to select and modify such a connected area starting from a specific point.
You are given a 2D integer array image representing a digital image. Each integer represents a pixel value (intensity). Given a starting pixel at coordinates (sr, sc) and a newColor, perform a "flood fill" on the image.
The flood fill operation should target the starting pixel, plus any pixels connected 4-directionally (horizontal or vertical) to the starting pixel that share the original color, and so on recursively. All targeted pixels must have their color replaced with newColor. You must return the modified image array.
Constraints:
image = [[1,1,1],[1,1,0],[1,0,1]] sr = 1, sc = 1 newColor = 2
[[2,2,2],[2,2,0],[2,0,1]]
Starting pixel is (1, 1) with color 1. All connected pixels with color 1 are changed to color 2.