zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    【JavaScript】关于javascript原型的深入理解
    【JavaScript】关于JS中的constructor与prototype
    【JavaScript】关于prototype
    【JavaScript】重温Javascript继承机制
    【JavaScript】新浪微博ajax请求后改变地址栏url,但页面不跳转的方案解析
    【JavaScript】JavaScript函数的参数
    【JavaScript】页面加载性能优化
    HTML5 修改浏览器url而不刷新页面
    【339】matplotlib based on python3
    【338】Pandas.DataFrame
  • 原文地址:https://www.cnblogs.com/kuroko-ghh/p/9363352.html
Copyright © 2011-2022 走看看