這是一道模擬題。說(shuō)白了就是石頭剪子布的問(wèn)題。此題的關(guān)鍵點(diǎn)是要開(kāi)兩個(gè)數(shù)組。一個(gè)是原來(lái)的。一個(gè)是改動(dòng)后的,每次改動(dòng)完之后都要用改動(dòng)之后的初始化原來(lái)的一次。下面請(qǐng)看代碼。我把函數(shù)分的比較詳細(xì)。便于大家閱讀。
#include<iostream>
using namespace std;
char array[102][102];
char temp[102][102];
int r,c,n;
void init()
{
int i,j;
for( i=1;i<=r;i++)
{
for( j=1;j<=c;j++)
{
array[i][j]=temp[i][j];
}
}
}
void change(int i,int j)
{
if(array[i][j]=='R')
{
if(i>1&&array[i-1][j]=='S')
temp[i-1][j]='R';
if(i+1<=r&&array[i+1][j]=='S')
temp[i+1][j]='R';
if(j>1&&array[i][j-1]=='S')
temp[i][j-1]='R';
if(j+1<=c&&array[i][j+1]=='S')
temp[i][j+1]='R';
}
else
if(array[i][j]=='P')
{
if(i>1&&array[i-1][j]=='R')
temp[i-1][j]='P';
if(i+1<=r&&array[i+1][j]=='R')
temp[i+1][j]='P';
if(j>1&&array[i][j-1]=='R')
temp[i][j-1]='P';
if(j+1<=c&&array[i][j+1]=='R')
temp[i][j+1]='P';
}
else
if(array[i][j]=='S')
{
if(i>1&&array[i-1][j]=='P')
temp[i-1][j]='S';
if(i+1<=r&&array[i+1][j]=='P')
temp[i+1][j]='S';
if(j>1&&array[i][j-1]=='P')
temp[i][j-1]='S';
if(j+1<=c&&array[i][j+1]=='P')
temp[i][j+1]='S';
}
}
void occupy()
{
int i,j;
while(n--)
{
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
change(i,j);
}
}
init();
}
}
int main()
{
int test;
cin>>test;
int i,j;
while(test--)
{
cin>>r>>c>>n;
for( i=1;i<=r;i++)
{
for( j=1;j<=c;j++)
{
cin>>array[i][j];
temp[i][j]=array[i][j];
}
}
/////////////OK 輸入字符完畢。
occupy();
for( i=1;i<=r;i++)
{
for( j=1;j<=c;j++)
{
cout<<array[i][j];
}
cout<<endl;
}
if(test!=0)
cout<<endl;
}
return 0;
}