24h購物| | PChome| 登入
2013-06-18 07:37:36| 人氣1,304| 回應0 | 上一篇 | 下一篇

[UVA][模擬] 10443 - Rock

推薦 0 收藏 0 轉貼0 訂閱站台

Problem E: Rock, Scissors, Paper

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. 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.

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

Output for Sample Input

RRR
RRR
RRR

RRRS
RRSP
RSPR


題目描述:
一個盤面上有剪刀石頭布,每過一個時間後,每個格子會跟鄰近的四個格子對決,
贏者將會佔領那隔,問 d 時間後的盤面。

題目解法:

類似生命遊戲,看每一格下一秒會變成什麼樣子,看鄰近有沒有贏過他的,如果有就替換。
#include <stdio.h>
#include <algorithm>
using namespace std;
char g[2][1024][1024];
int main() {
    int testcase;
    int n, m, d;
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%d %d %d", &n, &m, &d);
        while(getchar() != '\n');
        int i, j, k, flag = 0;
        for(i = 0; i < n; i++)
            gets(g[0][i]);
        while(d--) {
            for(i = 0; i < n; i++) {
                for(j = 0; j < m; j++) {
                    g[!flag][i][j] = g[flag][i][j];
                    if(g[flag][i][j] == 'R') {
                        if(i+1 < n && g[flag][i+1][j] == 'P')
                            g[!flag][i][j] = 'P';
                        else if(i-1 >= 0 && g[flag][i-1][j] == 'P')
                            g[!flag][i][j] = 'P';
                        else if(j+1 < m && g[flag][i][j+1] == 'P')
                            g[!flag][i][j] = 'P';
                        else if(j-1 >= 0 && g[flag][i][j-1] == 'P')
                            g[!flag][i][j] = 'P';
                    }
                    else if (g[flag][i][j] == 'P') {
                        if(i+1 < n && g[flag][i+1][j] == 'S')
                            g[!flag][i][j] = 'S';
                        else if(i-1 >= 0 && g[flag][i-1][j] == 'S')
                            g[!flag][i][j] = 'S';
                        else if(j+1 < m && g[flag][i][j+1] == 'S')
                            g[!flag][i][j] = 'S';
                        else if(j-1 >= 0 && g[flag][i][j-1] == 'S')
                            g[!flag][i][j] = 'S';
                    }
                    else {
                        if(i+1 < n && g[flag][i+1][j] == 'R')
                            g[!flag][i][j] = 'R';
                        else if(i-1 >= 0 && g[flag][i-1][j] == 'R')
                            g[!flag][i][j] = 'R';
                        else if(j+1 < m && g[flag][i][j+1] == 'R')
                            g[!flag][i][j] = 'R';
                        else if(j-1 >= 0 && g[flag][i][j-1] == 'R')
                            g[!flag][i][j] = 'R';
                    }
                }
            }
            flag = !flag;
        }
        for(i = 0; i < n; i++)
            g[flag][i][m] = '\0', puts(g[flag][i]);
        if(testcase)    puts("");
    }
    return 0;
}

台長: Morris
人氣(1,304) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][math] 10493 - Cats, with or without Hats
此分類上一篇:[UVA] 10446 - The Marriage Interview

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文