【poj2339】Rock, Scissors, Paper

【poj2339】Rock, Scissors, Paper

Description

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

Source

Waterloo local 2003.01.25
大意:给出一个矩阵,R吃S,S吃P,P吃R,向四周扩展,能吃掉的话就代替它,输出几次之后的矩阵结果
【代码】
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int sx[4]={1,-1,0,0};
int sy[4]={0,0,1,-1};
int T,n,m,k,x,y;
char a[110][110],b[110][110],s[110];
int main(){
	scanf("%d\n",&T);
	for (int t=1;t<=T;++t){
		scanf("%d%d%d\n",&n,&m,&k);
		for (int i=1;i<=n;++i){
			gets(s);
			for (int j=1;j<=m;++j){
				a[i][j]=s[j-1];
				b[i][j]=a[i][j];
			}
		}
		//printf("\n");
		/*for (int i=1;i<=n;++i){
			for (int j=1;j<=m;++j)
			  putchar(b[i][j]);
			printf("\n");
		}*/
		for (int r=1;r<=k;++r){
			for (int i=1;i<=n;++i)
			  for (int j=1;j<=m;++j)
			    a[i][j]=b[i][j];
			for (int i=1;i<=n;++i)
			  for (int j=1;j<=m;++j){
		  		switch(a[i][j]){
		  		  case 'S':{
		  			for (int l=0;l<4;++l){
		  			  x=i+sx[l];
		  			  y=j+sy[l];
		  			  if (x>0&&x<=n&&y>0&&y<=m)
		  			    if (a[x][y]=='P') b[x][y]='S';
		  			  }
		  			break;
		  		    }
		  		  case 'P':{
		  		  	for (int l=0;l<4;++l){
		  			  x=i+sx[l];
		  			  y=j+sy[l];
		  			  if (x>0&&x<=n&&y>0&&y<=m)
		  			    if (a[x][y]=='R') b[x][y]='P';
		  			  }
		  		  	break;
		  		    }
		  		  case 'R':{
		  		  	for (int l=0;l<4;++l){
		  			  x=i+sx[l];
		  			  y=j+sy[l];
		  			  if (x>0&&x<=n&&y>0&&y<=m)
		  			    if (a[x][y]=='S') b[x][y]='R';
		  			  }
		  		  	break;
		  		    }
		  	      }
		      }
	    }
	    for (int i=1;i<=n;++i){
			for (int j=1;j<=m;++j)
			  putchar(b[i][j]);
			printf("\n");
		}
		printf("\n");
	}
	return 0;
}


你可能感兴趣的:(模拟)