1 #include <iostream>
   2 #include <sstream>
   3 #include <string>
   4 #include <algorithm>
   5 #include <vector>
   6 using namespace std;
   7 
   8 #define GI ({int _t; scanf("%d", &_t); _t;})
   9 #define FOR(i, a, b) for (int i=a; i<b; i++)
  10 #define REP(i, a) FOR(i, 0, a)
  11 template<class T> string toString(T n){ostringstream ost;ost<<n;ost.flush();return ost.str();}
  12 int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;}
  13 #define DBG(x) cout << #x << "::" << x << endl;
  14 #define DBGV(_v) { REP(_i, _v.size()) { cout << _v[_i] << "\t";} cout << endl;}
  15 #define OK(a, b) ( a>=0 && b>=0 && a<5 && b<5 )
  16 
  17 int main() {
  18 	string line, commands;
  19 	int kase = 0;
  20 	bool first = true;
  21 	while (1) {
  22 		kase++;
  23 		vector <string> grid (5, "_____");
  24 		getline(cin, line);
  25 		if (line == "Z") break;
  26 		if (first == false) printf("\n");
  27 		first = false;
  28 		grid[0] = line;
  29 		FOR(i, 1, 5) {
  30 			getline(cin, line);
  31 			grid[i] = line;
  32 		}
  33 		commands = "";
  34 		while (1) {
  35 			getline(cin, line);
  36 			commands += line;
  37 			if (line[line.size()-1] == '0') break;
  38 		}
  39 		int x = -1, y = -1;
  40 		REP(i, 5) REP(j, 5) if (grid[i][j] == ' ') { x = i ; y = j; goto FOUND;}
  41 		FOUND: ;
  42 
  43 		int dx, dy;
  44 		bool flag = true;
  45 		REP(i, commands.size()-1) {
  46 			// cout << x << "\t" << y << "\t" << commands[i] << endl;
  47 			// REP(a, 5) { REP(b, 5) { printf("%c", grid[a][b]); } printf("\n"); } printf("\n\n");
  48 			if (commands[i] == 'A') { dx = -1; dy = 0;}
  49 			else if (commands[i] == 'B') { dx = 1; dy = 0;}
  50 			else if (commands[i] == 'L') { dx = 0; dy = -1;}
  51 			else if (commands[i] == 'R') { dx = 0; dy = 1;}
  52 			if (!OK(x+dx, y+dy)) {
  53 				flag = false;
  54 				break;
  55 			}
  56 			else {
  57 				swap(grid[x][y], grid[x+dx][y+dy]);
  58 				x += dx;
  59 				y += dy;
  60 			}
  61 		}
  62 		printf("Puzzle #%d:\n", kase);
  63 		if (flag == true) {
  64 			REP(i, 5) {
  65 				REP(j, 5) {
  66 					printf("%c", grid[i][j]);
  67 					if (j != 4) printf(" ");
  68 				}
  69 				printf("\n");
  70 			}
  71 		}
  72 		else {
  73 			printf("This puzzle has no final configuration.\n");
  74 		}
  75 	}
  76 	return 0;
  77 }