摇骰子
摇骰子。
#include<cstdio> inline bool valid(int x,int y,int n,char str[][55]){ return x>=0&&x<n&&y>=0&&y<n&&(str[x][y]=='#'||str[x][y]=='E'||str[x][y]=='S'); } void change1(int *a){ for(int i=4;i>=1;i--) a[i] = a[i-1]; a[0] = a[4]; } void change2(int *a){ a[4] = a[0]; for(int i=0;i<4;i++) a[i] = a[i+1]; } int main(){ int t,n; int sx,sy; char str[55][55]; scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i=0;i<n;i++) scanf("%s",str[i]); for(int i=0;i<n;i++) for(int j=0;j<n;j++) if(str[i][j]=='S'){ sx = i;sy = j; } int axis1[5] = {6,2,1,5},axis2[5]={6,3,1,4}; while(1){ char fin = str[sx][sy]; str[sx][sy] = axis1[0]+'0'; if(fin=='E')break; if(valid(sx,sy-1,n,str)){ sy--; axis2[0] = axis1[3]; axis2[2] = axis1[1]; change1(axis1); continue; } if(valid(sx,sy+1,n,str)){ sy++; axis2[0] = axis1[1]; axis2[2] = axis1[3]; change2(axis1); continue; } if(valid(sx-1,sy,n,str)){ sx--; axis1[0] = axis2[3]; axis1[2] = axis2[1]; change1(axis2); continue; } if(valid(sx+1,sy,n,str)){ sx++; axis1[0] = axis2[1]; axis1[2] = axis2[3]; change2(axis2); continue; } } for(int i=0;i<n;i++) puts(str[i]); } return 0; }