参考:https://blog.csdn.net/yzl_rex/article/details/7600906
https://blog.csdn.net/acm_JL/article/details/50200355
https://blog.csdn.net/qq_35040828/article/details/71123521
https://zhidao.baidu.com/question/49762862.html
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int N=105; 6 char old[N][N],news[N][N];//old存旧数组,news存新数组 7 int dir[2][4]={{-1,0,1,0},{0,1,0,-1}};//周围四个格 8 int r,l,n; 9 int lawful(int x,int y)//判断是否超出边界 10 { 11 if (x>=0&&x<r&&y>=0&&y<l) 12 { 13 return 1; 14 } 15 else 16 { 17 return 0; 18 } 19 } 20 char change(int x,int y)//依据规则改变 21 { 22 for (int i=0;i<4;i++) 23 { 24 int xx=x+dir[0][i]; 25 int yy=y+dir[1][i]; 26 if (lawful(xx,yy)) 27 { 28 if (old[xx][yy]=='R'&&old[x][y]=='S') 29 { 30 return 'R'; 31 } 32 else if (old[xx][yy]=='S'&&old[x][y]=='P') 33 { 34 return 'S'; 35 } 36 else if (old[xx][yy]=='P'&&old[x][y]=='R') 37 { 38 return 'P'; 39 } 40 } 41 } 42 return old[x][y]; 43 } 44 int main() 45 { 46 int t; 47 // freopen("bin.txt","r",stdin); 48 while (cin>>t) 49 { 50 while (t--) 51 { 52 cin>>r>>l>>n; 53 memset(old,0,sizeof(old)); 54 memset(news,0,sizeof(news)); 55 for (int i=0;i<r;i++) 56 { 57 for (int j=0;j<l;j++) 58 { 59 cin>>old[i][j]; 60 } 61 } 62 for (int i=0;i<n;i++) 63 { 64 for (int j=0;j<r;j++) 65 { 66 for (int k=0;k<l;k++) 67 { 68 news[j][k]=change(j,k);//更新news 69 } 70 } 71 memcpy(old,news,sizeof(news)); 72 } 73 for (int i=0;i<r;i++) 74 { 75 for (int j=0;j<l;j++) 76 { 77 cout<<old[i][j]; 78 } 79 cout<<endl; 80 } 81 cout<<endl; 82 } 83 } 84 85 return 0; 86 }