Bart's sister Lisa has created a new civilization on a two-dimensional grid. At the outset each grid location may be occupied by one of three life forms: Rocks, Scissors, or Papers. Each day, differing life forms occupying horizontally or vertically adjacent grid locations wage war. In each war, Rocks always defeat Scissors, Scissors always defeat Papers, and Papers always defeat Rocks. At the end of the day, the victor expands its territory to include the loser's grid position. The loser vacates the position.
Your job is to determine the territory occupied by each life form after n days.
Input
The first line of input contains t, the number of test cases. Each test case begins with three integers not greater than 100: r and c, the number of rows and columns in the grid, and n. The grid is represented by the r lines that follow, each with c characters. Each character in the grid is R, S, or P, indicating that it is occupied by Rocks, Scissors, or Papers respectively.
Output
For each test case, print the grid as it appears at the end of the nth day. Leave an empty line between the output for successive test cases.
Sample Input
2
3 3 1
RRR
RSR
RRR
3 4 2
RSPR
SPRS
PRSP
Sample Output
RRR
RRR
RRR
RRRS
RRSP
RSPR
这是一道模拟题目。
有一个关键点是:白天所发生的战争,得出结果,晚上再进行领土扩张。也就是说白天的战争已经有了结果,但是白天不能根据这个结果来进行战争,因为虽然打赢了这个战争,但是敌军仍然占领这座阵地。因为这条规则,才可以保证每天按任意顺序发生战争,得到的结果是一样的。迷惑的同学仔细揣摩下这句话吧,我就是在这里耽误半天,最后才恍然大悟。
所以白天发生的战争得到的结果,需要临时保存起来,晚上根据这个临时的结果进行领土扩张。
1 # include<stdio.h> 2 # include<string.h> 3 # define MAX 101 4 char map[MAX][MAX],temp[MAX][MAX]; //temp[MAXN][MAXN]用来存储临时结果 5 int r,c,n; 6 int xx[]={-1,0,0,1}; 7 int yy[]={0,-1,1,0}; 8 9 bool judge(int x,int y){ 10 if(x>=0 &&x<r &&y>=0 &&y<c) 11 return true; 12 return false; 13 } 14 15 void dfs(int x,int y){ 16 int i,a,b; 17 for(i=0;i<4;i++){ 18 a=x+xx[i]; b=y+yy[i]; 19 if(judge(a,b)){ //在map中进行战争,结果保存在temp中 20 if(map[x][y]=='R' &&map[a][b]=='S') temp[a][b]='R'; 21 else if(map[x][y]=='S' &&map[a][b]=='P') temp[a][b]='S'; 22 else if(map[x][y]=='P' &&map[a][b]=='R') temp[a][b]='P'; 23 } 24 } 25 } 26 27 int main(){ 28 int i,j,T,k; 29 scanf("%d",&T); 30 for(k=0;k<T;k++){ 31 if(k) printf("\n"); 32 memset(map,0,sizeof(map)); 33 scanf("%d%d%d",&r,&c,&n); 34 for(i=0;i<r;i++) 35 scanf("%s",map[i]); 36 memcpy(temp,map,sizeof(map)); 37 while(n--){ 38 for(i=0;i<r;i++){ 39 for(j=0;j<c;j++) 40 dfs(i,j); 41 } 42 memcpy(map,temp,sizeof(temp)); //晚上进行领土扩张 43 } 44 for(i=0;i<r;i++) 45 printf("%s\n",map[i]); 46 } 47 return 0; 48 }