问题类型:模拟,数组。
03pie’s solution for [UVA-227]:
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<string>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<vector>
#include<fstream>
#include<list>
using namespace std;#define ms(s) memset(s,0,sizeof(s))typedef unsigned long long ULL;
typedef long long LL;const double PI = 3.141592653589;
const int INF = 0x3fffffff;char frame[6][8];
char cmd[1000];int main(){
// freopen("F://inp.txt","r",stdin);int cases=0;for(;;){gets(frame[0]); if (frame[0][0] == 'Z') break; for (int i = 1 ; i < 5 ; ++ i) gets(frame[i]); int bx = 0,by = 0; for (int i = 0 ; i < 5 ; ++ i) for (int j = 0 ; j < 5 ; ++ j) if (frame[i][j] == ' ') { bx = i;by = j; break; } int count = 0; while (~scanf("%c",&cmd[count])) if (cmd[count] != '0') count ++; else break; cmd[count] = 0;getchar(); int ok=1;int x,y;for(int i=0;cmd[i];i++){switch (cmd[i]){case 'A': x=bx-1;y=by;break;case 'B': x=bx+1;y=by;break;case 'L': x=bx;y=by-1;break;case 'R': x=bx;y=by+1;break;}if(x<0||x>4||y<0||y>4){ok=0; break;}else{frame[bx][by]=frame[x][y];frame[x][y]=' ';bx=x;by=y;}}if (cases ++) printf("\n"); printf("Puzzle #%d:\n",cases); if (!ok) printf("This puzzle has no final configuration.\n"); else { for (int i = 0 ; i < 5 ; ++ i) { printf("%c",frame[i][0]); for (int j = 1 ; j < 5 ; ++ j) printf(" %c",frame[i][j]); printf("\n"); } } }return 0;
}