SDE Path

Flood Fill Algorithm

Easy

Flood Fill Algorithm

Given a starting pixel and new color, perform a flood fill on an image represented as a matrix.

Input format

The first line contains R, C, SR, SC, and NEWCOLOR. The next R lines contain the grid.

Output format

Perform flood fill starting at (SR, SC) with NEWCOLOR (4-directional). Print R lines of the filled grid.

Constraints

  • Values fit in a 64-bit signed integer
  • Trailing whitespace and a trailing newline are ignored by the judge

Read from stdin, write to stdout. Sample cases below show the exact format.

Sample cases

Example 1
Input
10 4
4 2 7 5
1 2 6 7
7 5 0 3
9 1 7 9
5 0 6 6
8 6 9 3
0 8 8 6
7 2 0 0
1 8 0 7
9 8 5 9
6 0 3
Expected output
4 2 7 5
1 2 6 7
7 5 0 3
9 1 7 9
5 0 6 6
8 6 9 3
3 8 8 6
7 2 0 0
1 8 0 7
9 8 5 9
Example 2
Input
8 7
8 4 4 7 0 5 1
5 0 9 1 1 2 8
0 5 7 8 1 7 4
9 2 0 0 4 2 8
1 8 6 6 5 3 7
3 8 1 6 1 6 9
1 2 6 2 8 9 9
2 6 2 7 1 9 2
1 3 7
Expected output
8 4 4 7 0 5 1
5 0 9 7 7 2 8
0 5 7 8 7 7 4
9 2 0 0 4 2 8
1 8 6 6 5 3 7
3 8 1 6 1 6 9
1 2 6 2 8 9 9
2 6 2 7 1 9 2