char a[7][7] = { 0 }, count = 0, gg = 0, x;
int main(void)
{
while (1)
{
int x0 = 0, y0 = 0, jd = 0;
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
scanf("%c", &a[i][j]);
if (a[1][1] == 'Z') return 0;
if (a[i][j] == ' ') { x0 = i; y0 = j; }
}
getchar();
}
while ((x=getchar())!='0')
{
if (x == 'A') { a[x0][y0] = a[x0 - 1][y0]; a[x0 - 1][y0] = ' '; x0--; }
if (x == 'B') { a[x0][y0] = a[x0 + 1][y0]; a[x0 + 1][y0] = ' '; x0++; }
if (x == 'L') { a[x0][y0] = a[x0][y0 - 1]; a[x0][y0 - 1] = ' '; y0--; }
if (x == 'R') { a[x0][y0] = a[x0][y0 + 1]; a[x0][y0 + 1] = ' '; y0++; }
if (x0 <= 0 || y0 <= 0 || x0 > 5 || y0 > 5)
jd = 1;
}
getchar();
if (gg)printf("\n");
else gg = 1;
if (jd)
printf("Puzzle #%d:\nThis puzzle has no final configuration.\n", ++count);
else
{
printf("Puzzle #%d:\n", ++count);
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
if (j != 1)printf(" ");
printf("%c", a[i][j]);
}
printf("\n");
}
}
}
return 0;
}