#include<stdio.h>
#include<string.h>
int main()
{
char c,s[10][35],e,b;
memset(s, '0', sizeof(s));
int i = 0, j = 0, i_1 = 0, j_1 = 0, book = 1, count = 0;
while (1)
{
book = 1, i = 0, j = 0;
memset(s, '0', sizeof(s));
while ( i < 5 && (c = getchar()) != EOF)
{
if (c == '\n')
{
i++;
j = 0;
continue;
}
else
{
if (j < 5)
{
if (c == ' ')
{
i_1 = i;
j_1 = j;
}
s[i][j++] = c;
}
}
if (c == 'Z')
return 0;
}
while ((c = getchar()) != '0')
{
if (c == '\n')
{
continue;
}
if (c == 'A')
{
if (i_1 - 1 < 0)
{
book = 0;
break;
}
else
{
e = s[i_1 - 1][j_1];
s[i_1 - 1][j_1] = s[i_1][j_1];
s[i_1][j_1] = e;
i_1 = i_1 - 1;
continue;
}
}
else if (c == 'B')
{
if (i_1 + 1 > 4)
{
book = 0;
break;
}
else
{
e = s[i_1 + 1][j_1];
s[i_1 + 1][j_1] = s[i_1][j_1];
s[i_1][j_1] = e;
i_1 = i_1 + 1;
continue;
}
}
else if (c == 'L')
{
if (j_1 - 1 < 0)
{
book = 0;
break;
}
else
{
e = s[i_1][j_1 - 1];
s[i_1][j_1 - 1] = s[i_1][j_1];
s[i_1][j_1] = e;
j_1 = j_1 - 1;
continue;
}
}
else if (c == 'R')
{
if (j_1 + 1 > 4)
{
book = 0;
break;
}
else
{
e = s[i_1][j_1 + 1];
s[i_1][j_1 + 1] = s[i_1][j_1];
s[i_1][j_1] = e;
j_1 = j_1 + 1;
continue;
}
}
else
{
book = 0;
}
}
while ((b=getchar()) != '\n') //此时缓存区被清空
continue;
if (count)
printf("\n");
if (book)
{
printf("Puzzle #%d:\n", ++count);
for (int x = 0; x < 5; x++)
{
for (int y = 0; y < 4; y++)
{
printf("%c ", s[x][y]);
}
printf("%c\n", s[x][4]);
}
}
else
{
printf("Puzzle #%d:\n", ++count);
printf("This puzzle has no final configuration.\n");
}
}
return 0;
}