• 2339Rock, 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
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define R 101
    #define C 101
    int main(int argc, char const *argv[])
    {
    	int n,i,j,k;
    	int r,c,t;
        char yest[R][C];
        char to[R][C];
        scanf("%d",&n);
        while(n--)
        { 
          scanf("%d%d%d",&r,&c,&t);
          getchar();
          for(i=1;i<=r;i++)
          { 	
          	for(j=1;j<=c;j++)
          	scanf("%c",&yest[i][j]);
            getchar();
          }
          		
          for(k=0;k<t;k++)
          {
          	memset(to,0,sizeof(to));
          	for(i=1;i<=r;i++)
          		{
          			for(j=1;j<=c;j++)
          			{
          				if(yest[i][j]=='R')
          					{
                               if(yest[i-1][j]=='P'||yest[i+1][j]=='P'||yest[i][j-1]=='P'||yest[i][j+1]=='P')
                               	   to[i][j]='P';
                               	else to[i][j]=yest[i][j];
          			        }
          			    else if (yest[i][j]=='P')
          			    {
                             if(yest[i-1][j]=='S'||yest[i+1][j]=='S'||yest[i][j-1]=='S'||yest[i][j+1]=='S')
                               	   to[i][j]='S';
                               	else to[i][j]=yest[i][j];
          			    }
          			    else if(yest[i][j]=='S')
          			    {
          			    	if(yest[i-1][j]=='R'||yest[i+1][j]=='R'||yest[i][j-1]=='R'||yest[i][j+1]=='R')
                               	   to[i][j]='R';
                               	else to[i][j]=yest[i][j];
          			    }
          		    }
          		}
             for(i=1;i<=r;i++)
             	for(j=1;j<=c;j++)
             		yest[i][j]=to[i][j];
          }
          for(i=1;i<=r;i++)
          {
          	for(j=1;j<=c;j++)
          	{
          		printf("%c",to[i][j]);
          	}
          	printf("
    ");
          }
          printf("
    ");
        }
    
    	return 0;
    }

  • 相关阅读:
    设置linux查看历史命令显示执行时间
    CentOS7.6操作系统安装实例以及Linux版本、哲学思想介绍
    JavaScript 数据结构1
    原生js 正则表达
    js Event事件
    引用类型: 归并方法
    引用类型: 迭代方法
    引用类型 位置方法 indexOf()和 lastIndexOf()
    引用类型 操作方法
    引用类型 重排序方法
  • 原文地址:https://www.cnblogs.com/kuroko-ghh/p/9363352.html
Copyright © 2020-2023  润新知