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;
    }

  • 相关阅读:
    线索二叉树
    正则表达式之后向引用
    进步的阶梯
    树和二叉树
    java 执行 exe 文件
    Electron + Vue如何实现不同窗口之间的通信(项目总结 第一个)
    liunx 修改 ip 地址
    桌面快捷工具
    微信小程序长列表组件 recycle-view 修改,使其可以下拉刷新
    微信小程序 textarea 文本滚动不了的bug
  • 原文地址:https://www.cnblogs.com/kuroko-ghh/p/9363352.html
Copyright © 2011-2022 走看看