Loading...
Loading...
Ananya is making a poster for her college club in a simple paint app. The picture is a grid of r rows and c columns. Each cell holds one colour, written as a digit from 0 to 9.
She picks the bucket tool and clicks the cell in row sr, column sc with the new colour k. The clicked cell changes to k. So does every cell that can be reached from it by moving up, down, left or right through cells of the same old colour as the clicked cell. Cells that only touch diagonally are not connected.
Print the picture after the click.
Example 1
Input
3 4 1110 1101 0011 1 1 2
Output
2220 2201 0011
The five 1s joined to the top-left corner become 2. The three 1s at the bottom right are a separate patch, so they stay.
Example 2
Input
2 2 55 55 2 2 5
Output
55 55
The new colour is the same as the old one, so nothing changes.
Try the problem first. Open one hint at a time.
Read the old colour of the clicked cell first, before you change anything.
From a cell of the old colour, look at its four neighbours. Each neighbour inside the grid that still has the old colour belongs to the patch.
Changing a cell to k also marks it as done, because it no longer has the old colour.
That marking fails when k equals the old colour. Handle that case first: the picture stays the same.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor