11239 - Pour Water   

Description

A farmer has a crop land. The land consists of stones (S) and holes (H) as illustrated in the following figure. Note that there is only one hole on the land’s first row.

S

S

H

S

S

H

S

H

H

H

H

H

H

S

S

H

H

S

H

H

H

S

S

S

S

 

The farmer decides to water his crops, so he pours some water through the hole on the first row into the land. Assume that the amount of water is infinite, and the water can move in the four directions: up, down, left, right. Please write a program to find out where the water would reach.

For example, you are given a coordinate (0,2) representing the entrance and a table representing the farmer’s land initially:

S

S

H

S

S

H

S

H

H

H

H

H

H

S

S

H

H

S

H

H

H

S

S

S

S

 

After the farmer pours water into the land, water floods throughout the holes of the land. Finally, a portion of the holes will be filled with water like the following table.

S

S

W

S

S

W

S

W

W

W

W

W

W

S

S

W

W

S

H

H

W

S

S

S

S

 

where W means the corresponding hole is filled with water.

You will be provided with the following sample code, and asked to implement function "flooding".

#include <stdio.h>

void flooding(int R, int C, int pr, int pc);
char Map[505][505];

int main(void){
    int N,R,C,X,i,j,k;
    scanf("%d", &N);
    for(k=0;k<N;k++){
        scanf("%d%d%d", &R, &C, &X);

            for(i=0;i<R;i++){
                for(j=0;j<C;j++){
                    Map[i][j] = ' ';
                }
            }
            for(i=0;i<R;i++){
                scanf("%s", Map[i]);
            }

            flooding(R,C,0,X);
            for(i=0;i<R;i++){
                for(j=0;j<C;j++){
                    printf("%c", Map[i][j]);
                }
                printf("\n", Map[i][j]);
            }
            if(k<N-1)
                printf("\n", Map[i][j]);
    }
    return 0;
}

void flooding(int R, int C, int pr, int pc){
    /*your code*/
}

Input

The first line has an integer N(1<=N<=100), which means the number of test cases.

For each case, the first line has three integers. The first and second integers R and C (3<=R, C<=500) represent the numbers of row and column of the land, respectively. The total number of elements in the land is thus R x C. The third integer X (0<=X<=C-1) means the column of the entrance at the first row. The coordinate of the entrance is thus (0,X). The following R lines, each containing C characters, specify the elements of the farmer’s land.

Output

Print out all elements of the lands row-by-row, and there is a '\n' at the end of each line. The states of all lands should be separated by a new line character (\n).

Sample Input  Download

Sample Output  Download

Tags




Discuss