#include<iostream>
#include<string.h>
#include<ctype.h>
#include<iomanip>
#include<stdio.h>
using namespace std;
void search(char st[],int m,int n);
int main(void)
{
int r,p=0,m=0,n=0,time=0;
char st[5][4];
int a,b;
char ope[99];
char button;
while(1)
{time++;
memset(st,0,sizeof(st));
for(r=1; r<=5; r++)
{
st[r]=getchar();
if(st[1][0]=='Z')
{
p=1;
break;
}
}
if(p)
break;
search(st,m,n);
while((button=getchar())!='0')
{
switch(button)
{case 'A':
st[m][n]=st[m-1][n];
st[m-1][n]=' ';
m=m-1;
break;
case'B':
st[m][n]=st[m+1][n];
st[m+1][n]=' ';
m=m+1;
break;
case'R':
st[m][n]=st[m][n+1];
st[m][n+1]=' ';
n=n+1;
break;
case'L':
st[m][n]=st[m][n-1];
st[m][n-1]=' ';
n=n-1;
break;}
}
cout<<"Puzzle #"<<time<<":"<<endl;
if(m*m<25&&n*n<25){
for(a=1;a<=5;a++)
for(b=0;b<=4;b++)
{if(b<4)cout<<st[a][b]<<' ';
if(b==4)cout<<st[a][b]<<endl;
}
cout<<endl;}
else cout<<"This puzzle has no final configuration."<<endl<<endl;}
return 0;
}
void search(char st[],int m,int n)
{int a,b,stop=0;
for(a=1;a<=5;a++)
{
for(b=0;b<=4;b++)
{if(st[a][b]==' '){m=a;n=b;break;stop=1;}}
if(stop)break;}
}
onemoo
onemoo