Flood Fill Image

Easy

Given an image as a matrix of colored cells. Each cell has a value ranging from 0 to 65535 denoting its color. You have to apply flood-fill to a particular cell of the matrix with color c.

Two cells are considered as part of the same connected component if they have a common side and the same color value.

When you apply flood-fill to a particular cell, all its connected components are also applied the same color.

Flood Fill Image

Testing

Input Format

The first line contains an integer ‘T’, denoting the number of test cases.

For each test case the input has the following lines:

  • The first line contains two space separated numbers, ‘n’ and ‘m’, denoting the number of rows and columns of the matrix.
  • The next n lines contain m space-separated integers denoting the values at respective cells of the matrix.
  • The next line contains two integers ‘x’ and ‘y’ denoting the target row and column index of the cell to which flood fill is to be applied.
  • The next line contains an integer ‘c’, denoting the color to fill.

Output Format

For each test case, the output contains n lines containing m space-separated integers denoting the resultant image.

Sample Input

4
2 2
1 0
0 0
1 1
2
3 3
1 1 0
1 1 1
1 1 1
0 0
3
4 4
1 2 1 2
2 2 2 1
1 2 2 1
2 1 2 1
0 1
3
2 2
1 2
2 1
0 1
3

Expected Output

1 2
2 2
3 3 0
3 3 3
3 3 3
1 3 1 2
3 3 3 1
1 3 3 1
2 1 3 1
1 3
2 1

Constraints

1 <= T <= 10
1 <= n, m <= 500
0 <= color value <= 65535
0 <= x < n
0 <= y < m
0 <= c <= 65535

Companies
Editorial Link: Editorial