By Diego Pereira da Penha, Instituto Federal do Sul de Minas Gerais Brazil
Laval Ianos Tinta was a professional painter, who made several paintings to sell. One day, he noticed that he used a lot of paint to paint his pictures. To avoid wasting ink, he thought of creating a simulator, and he wants your help to develop such a simulator. The frame is represented by a numerical matrix, where each number represents a color. Such a simulator must have the fill tool, which changes the color of a region of the frame to a different color, respecting the limits of the other regions. In the following image, we have an example of a frame before running the fill tool; then the same frame appears with the position of the fill tool, represented by a star, already with the new color to fill the designated region; and how the picture was after such filling.
Your task is to develop an algorithm that performs the function of the frame fill tool. The painter has already given a name to such a tool: It's the Comb!
The input has several test cases. Each case starts with two numbers, C and L, representing the length and width of the frame. Then, C lines with L numbers will be informed, separated by a space, each number representing an ink color. Finally, there are three numbers: X, Y and N, representing the line (X) and column (Y) coordinates where the fill tool should be positioned, and the value of the new color to fill that region (N).
The output should print as the frame looked after using the fill tool. The following example is based on the previous image sequence.
Input Sample | Output Sample |
8 8 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0 0 1 1 0 1 1 1 2 2 2 2 0 1 0 1 1 1 2 2 0 1 0 1 1 1 2 2 1 2 0 1 1 1 1 1 2 1 1 1 1 1 1 1 2 2 1 4 4 3 |
1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0 0 1 1 0 1 1 1 3 3 3 3 0 1 0 1 1 1 3 3 0 1 0 1 1 1 3 3 1 2 0 1 1 1 1 1 2 1 1 1 1 1 1 1 2 2 1 |