Move函数写得太菜了···一个switch可以减少很多代码量···
单纯模拟:代码思路清晰
#define LOCAL
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
char Puzzle[5][7];
int X,Y;
string Inst;
int Round=0;
int NoFinal=0;
void Move(){
for(int i=0; i<Inst.length(); i++){
if(Inst[i]=='A') {
if(X-1<0) {
NoFinal=1; return;
}
swap(Puzzle[X-1][Y],Puzzle[X][Y]);
X--;
}
if(Inst[i]=='B') {
if(X+1>4) {
NoFinal=1; return;
}
swap(Puzzle[X+1][Y],Puzzle[X][Y]);
X++;
}
if(Inst[i]=='L') {
if(Y-1<0){
NoFinal=1; return;
}
swap(Puzzle[X][Y-1],Puzzle[X][Y]);
Y--;
}
if(Inst[i]=='R') {
if(Y+1>4) {
NoFinal=1; return;
}
swap(Puzzle[X][Y+1],Puzzle[X][Y]);
Y++;
}
}
}
void Display(){
if(Round++) printf("\n");
printf("Puzzle #%d:\n",Round);
if(NoFinal){
printf("This puzzle has no final configuration.\n");
return ;
}
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
if(j)
printf(" %c",Puzzle[i][j]);
else
printf("%c",Puzzle[i][j]);
}
printf("\n");
}
}
int main()
{
#ifdef LOCAL
freopen("in1.txt","r",stdin);
#endif
while(1)
{
NoFinal=0;
int End=0;
for(int i=0; i<5; i++)
{
for(int j=0; j<6; j++){
Puzzle[i][j]=getchar();
if(Puzzle[i][j]==' '){
X=i; Y=j;
}
if(Puzzle[i][j]=='Z'){
End=1; break;
}
}
if(End) break;
}//for
if(End) return 0;
Inst=""; char C;
while( (C=getchar())!='0' ){
if(C=='\n') continue;
Inst+=C;
}
getchar();
Move();
Display();
}//while
return 0;
}