# UVa227题解（算法竞赛入门经典习题3-5）

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

int main()
{
char puzzle[6][6];
int line=0,row=0;
int cas=0;
while(1)
{
line=0;
row=0;
cas++;
bool flag=true;
memset(puzzle,0,sizeof(puzzle));
puzzle[0][0]=getchar();
if(puzzle[0][0]=='Z') break;
else
{
for(int i=1;i<5;i++)
{
puzzle[0][i]=getchar();
if(puzzle[0][i]==' ') row=i;
}
getchar();
for(int i=1;i<5;i++)
{
for(int j=0;j<5;j++)
{
puzzle[i][j]=getchar();
if(puzzle[i][j]==' ')
{
line=i;
row=j;
}
}
getchar();
}
}
char mov;
scanf("%c",&mov);
while(mov!='0')
{
if(mov=='A')
{
if(line==0) flag=false;
else
{
puzzle[line][row]=puzzle[line-1][row];
puzzle[line-1][row]=' ';
line=line-1;
}
}
if(mov=='B')
{
if(line==4) flag=false;
else{
puzzle[line][row]=puzzle[line+1][row];
puzzle[line+1][row]=' ';
line=line+1;
// cout<<"line: "<<line<<" row:"<<row<<endl;
}
}
if(mov=='L')
{
if(row==0) flag=false;
else
{
puzzle[line][row]=puzzle[line][row-1];
puzzle[line][row-1]=' ';
row=row-1;
//cout<<"line: "<<line<<" row:"<<row<<endl;
}
}
if(mov=='R')
{
if(row==4) flag=false;
else
{
puzzle[line][row]=puzzle[line][row+1];
puzzle[line][row+1]=' ';
row=row+1;
//cout<<"line: "<<line<<" row:"<<row<<endl;
}
}
scanf("%c",&mov);
}
getchar();
if(cas!=1) cout<<endl;
cout<<"Puzzle #"<<cas<<":"<<endl;
if(flag)
{
for(int i=0;i<5;i++)
{
for(int j=0;j<4;j++)
cout<<puzzle[i][j]<<" ";
cout<<puzzle[i][4]<<endl;
}

}
else cout<<"This puzzle has no final configuration."<<endl;
}
}


oj上时间是40ms