题目来源:http://poj.org/problem?id=2339
小模拟,注意字母S的大小,为此wa了几次!
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAXN = 110;
int main()
{
char Graph1[MAXN][MAXN], Graph2[MAXN][MAXN];
int T, row, col, days, i, j;
scanf("%d", &T);
while(T--)
{
scanf("%d %d %d", &row, &col, &days);
memset(Graph1, 0, sizeof(Graph1));
memset(Graph2, 0, sizeof(Graph2));
for(i = 0; i < row; ++i)
{
scanf("%s", Graph1[i]);
for(j = 0; j < col; ++j)
Graph2[i][j] = Graph1[i][j];
Graph2[i][j] = '\0';
}
while(days--)//处理这几天的变化状态
{
for(i = 0; i < row; ++i)
{
for(j = 0; j < col; ++j)
{
switch(Graph1[i][j])
{
case 'R'://判断R的上下左右是否为S,如果是,则用R替换掉S,以下类似
if(j-1 >= 0 && Graph1[i][j-1] == 'S')
Graph2[i][j-1] = 'R';
if(j+1 < col && Graph1[i][j+1] == 'S')
Graph2[i][j+1] = 'R';
if(i-1 >= 0 && Graph1[i-1][j] == 'S')
Graph2[i-1][j] = 'R';
if(i+1 < row && Graph1[i+1][j] == 'S')
Graph2[i+1][j] = 'R';
break;
case 'S':
if(j-1 >= 0 && Graph1[i][j-1] == 'P')
Graph2[i][j-1] = 'S';
if(j+1 < col && Graph1[i][j+1] == 'P')
Graph2[i][j+1] = 'S';
if(i-1 >= 0 && Graph1[i-1][j] == 'P')
Graph2[i-1][j] = 'S';
if(i+1 < row && Graph1[i+1][j] == 'P')
Graph2[i+1][j] = 'S';
break;
case 'P':
if(j-1 >= 0 && Graph1[i][j-1] == 'R')
Graph2[i][j-1] = 'P';
if(j+1 < col && Graph1[i][j+1] == 'R')
Graph2[i][j+1] = 'P';
if(i-1 >= 0 && Graph1[i-1][j] == 'R')
Graph2[i-1][j] = 'P';
if(i+1 < row && Graph1[i+1][j] == 'R')
Graph2[i+1][j] = 'P';
break;
default:
break;
}
}
}
for(i = 0; i < row; ++i)
strcpy(Graph1[i], Graph2[i]);
}
for(i = 0; i < row; ++i)
printf("%s\n", Graph1[i]);
if(T)
printf("\n");
}
return 0;
}