您的位置:首页 > 其它

POJ 2339 Rock, Scissors, Paper 模拟

2014-04-19 22:59 423 查看
Rock, Scissors, Paper

Time Limit: 5000MSMemory Limit: 65536K
Total Submissions: 3730Accepted: 2181
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

Waterloo local 2003.01.25
#include <iostream>
#include <string>
using namespace std;

char grid[110][110];
char tmp[110][110];

int main()
{
int tc, r, c, n, i, j;
cin >> tc;
while (tc--)
{
cin >> r >> c >> n;
for (i = 1; i <= r; i++)
for (j = 1; j <= c; j++){
cin >> grid[i][j];
tmp[i][j] = grid[i][j];
}

while (n--){
for (i = 1; i <= r; i++)
{
for (j = 1; j<= c; j++)
{
if (grid[i][j] == 'R')
{
if (i-1 > 0 && grid[i-1][j] == 'S')
tmp[i-1][j] = 'R';
if (j-1 > 0 && grid[i][j-1] == 'S')
tmp[i][j-1] = 'R';
if (i+1 <= r && grid[i+1][j] == 'S')
tmp[i+1][j] = 'R';
if (j+1 <= c && grid[i][j+1] == 'S')
tmp[i][j+1] = 'R';
}
else if (grid[i][j] == 'S')
{
if (i-1 > 0 && grid[i-1][j] == 'P')
tmp[i-1][j] = 'S';
if (j-1 > 0 && grid[i][j-1] == 'P')
tmp[i][j-1] = 'S';
if (i+1 <= r && grid[i+1][j] == 'P')
tmp[i+1][j] = 'S';
if (j+1 <= c && grid[i][j+1] == 'P')
tmp[i][j+1] = 'S';
}
else if (grid[i][j] == 'P')
{
if (i-1 > 0 && grid[i-1][j] == 'R')
tmp[i-1][j] = 'P';
if (j-1 > 0 && grid[i][j-1] == 'R')
tmp[i][j-1] = 'P';
if (i+1 <= r && grid[i+1][j] == 'R')
tmp[i+1][j] = 'P';
if (j+1 <= c && grid[i][j+1] == 'R')
tmp[i][j+1] = 'P';
}
}
}
for (i = 1; i <= r; i++)
for (j = 1; j <= c; j++)
grid[i][j] = tmp[i][j];
}

for (i = 1; i <= r; i++)
{
for (j = 1; j <= c; j++)
cout << grid[i][j];
cout << endl;
}
if (tc != 0)
cout << endl;
}
return 0;
}


kdwycz的网站: http://kdwycz.com/

kdwyz的刷题空间:http://blog.csdn.net/kdwycz
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: