#C7280. Flood Fill Algorithm

    ID: 51134 Type: Default 1000ms 256MiB

Flood Fill Algorithm

Flood Fill Algorithm

You are given a two-dimensional grid, an initial starting point, and a new color. Your task is to perform a flood fill: replace the color of the starting pixel and all adjacent pixels (vertically and horizontally) that have the same original color with the new color.

Let \(G\) be an \(m \times n\) grid. A cell \(G(i,j)\) is filled if it is connected to \(G(sr,sc)\) (i.e. there exists a path from \(G(sr,sc)\) to \(G(i,j)\)) where every cell on the path has the same color as \(G(sr,sc)\). The new grid is then printed after performing the fill.

inputFormat

The input is provided from standard input (stdin) in the following format:

m n new_color
row1\_col1 row1\_col2 ... row1\_coln
row2\_col1 row2\_col2 ... row2\_coln
... (m rows in total)
sr sc

Where:

  • m: Number of rows in the grid.
  • n: Number of columns in the grid.
  • new_color: The integer value of the new color to be used in the flood fill.
  • Each row of the grid contains n integers.
  • sr and sc indicate the starting row and column indices respectively (0-indexed).

outputFormat

Print the modified grid to standard output (stdout), where each row is printed on a separate line with the values separated by a space.

## sample
3 4 3
1 1 1 2
1 2 2 2
1 1 1 2
0 0
3 3 3 2

3 2 2 2 3 3 3 2

</p>