#K50302. Flood Fill Algorithm
Flood Fill Algorithm
Flood Fill Algorithm
You are given a 2D grid of integers representing colors, and three integers: sr, sc and newColor. The pixel at position (sr, sc) is the starting point for a flood fill operation. Replace the color of the starting pixel and all adjacent pixels (4-directionally) that have the same original color with newColor.
The algorithm should work as follows:
- Determine the original color at position (sr, sc).
- If the original color is the same as newColor, the grid remains unchanged.
- Otherwise, change the color of the starting pixel and recursively all adjacent pixels (up, down, left, right) having the original color.
You need to implement the flood fill algorithm based on the description provided.
inputFormat
The input is read from standard input and formatted as follows:
- The first line contains two integers m and n, representing the number of rows and columns in the grid.
- The next m lines each contain n integers separated by spaces, representing the grid.
- The last line contains three integers: sr, sc, and newColor.
outputFormat
The output should be printed to standard output as m lines, where each line contains n integers (separated by a single space) representing the grid after performing the flood fill operation.
## sample3 3
1 1 1
1 1 0
1 0 1
1 1 2
2 2 2
2 2 0
2 0 1
</p>