Implement a flood fill algorithm on a given 2D grid, starting from a specified position (sr,sc), to replace all connected pixels with the same original value with a new value. The connection between pixels is defined by 4-connectivity, where two pixels are considered connected if they are adjacent horizontally or vertically.
The flood fill operation is a fundamental concept in image segmentation, which is crucial in computer vision for separating objects or regions of interest from the rest of the image. This process can be viewed as a graph traversal problem, where each pixel represents a node, and the connections between them are edges. The goal is to traverse the graph, starting from a given node, and update all connected nodes that share the same original value.
Here are the steps to achieve this:
This technique is widely used in image editing software.
grid = [[1, 1, 1], [1, 1, 0], [1, 0, 1]] sr = 1, sc = 1, new_val = 2
[[2, 2, 2], [2, 2, 0], [2, 0, 1]]