POJ2339 ZOJ1921 UVA10443 Rock, Scissors, Paper【Ad Hoc】

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 4301 Accepted: 2490

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


问题链接POJ2339 ZOJ1921 UVA10443 Rock, Scissors, Paper

问题简述:(略)

问题分析

  在r行c列的矩阵上玩石头剪刀布游戏,玩n次,求最后的结果。

  用模拟来实现。

程序说明:(略)

题记:(略)

参考链接:(略)


AC的C++语言程序如下:
/* POJ2339 ZOJ1921 UVA10443 Rock, Scissors, Paper */

#include <iostream>
#include <string.h>

using namespace std;

const int N = 100;
char grid[N][N], tmp[N][N];
int r, c, n;

const int drow[] = {-1, 0, 1, 0};
const int dcol[] = {0, -1, 0, 1};
const int LEN = sizeof(drow) / sizeof(int);

void play(int row, int col)
{
    if(grid[row][col] == 'R') {
        for(int i = 0; i < LEN; i++) {
            int nextrow = row + drow[i];
            int nextcol = col + dcol[i];
            if(nextrow < 0 || nextrow >= r || nextcol < 0 || nextcol >= c)
                continue;

            if(grid[nextrow][nextcol] == 'S')
                tmp[nextrow][nextcol] = 'R';
        }
    } else if(grid[row][col] == 'S') {
        for(int i = 0; i < LEN; i++) {
            int nextrow = row + drow[i];
            int nextcol = col + dcol[i];
            if(nextrow < 0 || nextrow >= r || nextcol < 0 || nextcol >= c)
                continue;

            if(grid[nextrow][nextcol] == 'P')
                tmp[nextrow][nextcol] = 'S';
        }
    } else if(grid[row][col] == 'P') {
        for(int i = 0; i < LEN; i++) {
            int nextrow = row + drow[i];
            int nextcol = col + dcol[i];
            if(nextrow < 0 || nextrow >= r || nextcol < 0 || nextcol >= c)
                continue;

            if(grid[nextrow][nextcol] == 'R')
                tmp[nextrow][nextcol] = 'P';
        }
    }
}

int main()
{
    int t;
    cin >> t;
    while(t--) {
        cin >> r >> c >> n;
        for(int i = 0; i < r; i++)
            for(int j = 0; j < c; j++)
                cin >> tmp[i][j];

        for(int i = 1; i <= n; i++) {
            memcpy(grid[0], tmp[0], sizeof(grid));

            for(int i = 0; i < r; i++)
                for(int j = 0; j < c; j++)
                        play(i, j);
        }

        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++)
                cout << tmp[i][j];
            cout << endl;
        }
        if(t)
            cout << endl;
    }

    return 0;
}





阅读更多
所属专栏: ACM题解
想对作者说点什么? 我来说一句
相关热词

没有更多推荐了,返回首页

关闭
关闭
关闭