#include<stdio.h>
#include<time.h>
#include<math.h>
#include<iomanip>
#include<string.h>
#include<iostream>
using namespace std;
const int maxsize=5;
char map[maxsize][maxsize];
bool check(int x,int y)
{
if(x>=0 &&x<5 &&y>=0 &&y<5)
{
return true;
}
else return false;
}
int main()
{
char c1;
int count=1; //计数
c1=getchar();
while(c1!='Z')
{
//cout<<"c1="<<c1<<endl;
int sx,sy;
for(int i=0;i<5;i++)
{//输入地图
for(int j=0;j<5;j++)
{
if(i == 0 && j== 0)
{
map[0][0]=c1;
if(map[0][0] == ' ')
{
sx=i;
sy=j;
}
}
else
{
char c2=getchar();
if(c2 == '\n' && j == 0)
{
j--;
}
else if(c2 == '\n' && j == 4)
{
map[i][j]=' ';
sx=i;
sy=j;
break;
}
else if(c2 !='\n')
{
map[i][j]=c2;
if(map[i][j] == ' ')
{
sx=i;
sy=j;
}
}
}
}
}
/*for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
cout<<map[i][j];
}
cout<<endl;
}*/
//<<"sx="<<sx<<" sy="<<sy<<endl;
//c1=getchar(); //把输入地图之后剩下的换行符给读出来;
//cout<<"c1="<<c1<<endl;
char order[5000];
int i1=0;
char c3=getchar();
while(c3 != '0')
{
if(c3 !='\n')
{
if(c3 =='A' || c3=='B' || c3 == 'L' || c3 == 'R')
order[i1]=c3;
else
{
}
//cout<<order[i1]<<endl;
i1++;
}
c3=getchar();
}
c3=getchar(); //把0之后的换行符给读出来
int i2=0;
while(i2<i1)
{
if(order[i2] == 'A')
{//上
map[sx][sy]=map[sx-1][sy];
map[sx-1][sy]=' ';
sx=sx-1;
sy=sy;
if(!check(sx,sy))
{
cout<<"Puzzle #"<<count<<":"<<endl;
count++;
cout<<"This puzzle has no final configuration."<<endl;
i1=0;
break;
}
}
else if(order[i2] == 'B')
{//下
map[sx][sy]=map[sx+1][sy];
map[sx+1][sy]=' ';
sx=sx+1;
sy=sy;
if(!check(sx,sy))
{
cout<<"Puzzle #"<<count<<":"<<endl;
count++;
cout<<"This puzzle has no final configuration."<<endl;
i1=0;
break;
}
}
else if(order[i2] == 'L')
{//左
map[sx][sy]=map[sx][sy-1];
map[sx][sy-1]=' ';
sx=sx;
sy=sy-1;
if(!check(sx,sy))
{
cout<<"Puzzle #"<<count<<":"<<endl;
count++;
cout<<"This puzzle has no final configuration."<<endl;
sx=sx;
sy=sy+1;
i1=0;
break;
}
}
else if(order[i2] == 'R')
{//右
map[sx][sy]=map[sx][sy+1];
map[sx][sy+1]=' ';
sx=sx;
sy=sy+1;
if(!check(sx,sy))
{
cout<<"Puzzle #"<<count<<":"<<endl;
count++;
cout<<"This puzzle has no final configuration."<<endl;
i1=0;
break;
}
}
i2++;
}
if(i2 == i1)//证明此时所有指令都成功执行并且未非法
{
cout<<"Puzzle #"<<count<<":"<<endl;
count++;
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
if(j == 0)
{
cout<<map[i][j];
}
else
{
cout<<" "<<map[i][j];
}
}
cout<<endl;
}
cout<<endl;
}
c1=getchar();
}
return 0;
}