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

  • 相关阅读:
    第四季-专题11-LED驱动程序设计
    第四季-专题12-按键驱动程序设计
    第四季-专题9-Linux驱动开发前奏
    第四季-专题10-字符设备驱动模型
    第四季-专题7-Linux内核链表
    第四季-专题8-LINUX系统调用
    第四季-专题6-Linux内核子系统
    第四季-专题5-内核模块开发
    第四季-专题3-嵌入式Linux内核制作
    jquery js获取移动设备浏览器高度
  • 原文地址:https://www.cnblogs.com/kuroko-ghh/p/9363352.html
Copyright © 2011-2022 走看看