1 #include <iostream>
2 #include <stdlib.h>
3 #include <string>
4 #include <vector>
5 #include <algorithm>
6 #include <string.h>
7 #include <stack>
8 #include <unordered_map>
9 #include <math.h>
10 #include <iomanip>
11
12 using namespace std;
13
14 int main()
15 {
16 char puzzleGraph[5][7];
17 string inputLine;
18 int cnt = 1;
19 while(1)
20 {
21 int flag = 0,flag2 = 1;
22 int cur_x,cur_y;
23
24 for(int i = 0; i < 5; i ++)
25 {
26 if(flag2 && cnt!=1)
27 {
28 getline(cin, inputLine);
29 i = -1;
30 flag2 = 0;
31 continue;
32 }
33 getline(cin, inputLine);
34 if(i==0 && inputLine[0] == 'Z')
35 {
36 flag = 1;
37 break;
38 }
39 for(int j = 0; j < 5; j ++)
40 {
41 if(inputLine[j] == ' ')
42 {
43 cur_x = i;
44 cur_y = j;
45 // cout << "OK" << endl;
46 }
47 if(inputLine.size()==4 && j==3)
48 {
49 puzzleGraph[i][j] = inputLine[j++];
50 // puzzleGraph[i][++j] = ' ';
51 cur_x = i;
52 cur_y = 4;
53 continue;
54 }
55 puzzleGraph[i][j] = inputLine[j];
56 }
57 }
58
59 if(flag)
60 break;
61
62 string actions;
63 cin >> actions;
64 while(actions[actions.size()-1] != '0')
65 {
66 string tmp;
67 cin >> tmp;
68 actions += tmp;
69 }
70
71 for(int i = 0; actions[i]!='0'; i ++)
72 {
73 // cout << cur_x << " " << cur_y << endl;
74 if(actions[i]=='A')
75 {
76 if(cur_x-1>=0)
77 {
78 puzzleGraph[cur_x][cur_y] = puzzleGraph[cur_x-1][cur_y];
79 cur_x --;
80 }
81 else
82 {
83 flag = 3;
84 break;
85 }
86 }
87 else if(actions[i]=='B')
88 {
89 if(cur_x+1<5)
90 {
91 puzzleGraph[cur_x][cur_y] = puzzleGraph[cur_x+1][cur_y];
92 cur_x ++;
93 }
94 else
95 {
96 flag = 3;
97 break;
98 }
99 }
100 else if(actions[i]=='R')
101 {
102 if(cur_y+1<5)
103 {
104 puzzleGraph[cur_x][cur_y] = puzzleGraph[cur_x][cur_y+1];
105 cur_y ++;
106 }
107 else
108 {
109 flag = 3;
110 break;
111 }
112 }
113 else if(actions[i]=='L')
114 {
115 if(cur_y-1>=0)
116 {
117 puzzleGraph[cur_x][cur_y] = puzzleGraph[cur_x][cur_y-1];
118 cur_y --;
119 }
120 else
121 {
122 flag = 3;
123 break;
124 }
125 }
126 }
127 if(cnt!=1)
128 cout << endl;
129 cout << "Puzzle #" << cnt ++ << ":" << endl;
130 if(flag == 3)
131 {
132 cout << "This puzzle has no final configuration." << endl;
133 continue;
134 }
135
136 puzzleGraph[cur_x][cur_y] = ' ';
137
138 for(int i = 0; i < 5; i ++)
139 {
140 for(int j = 0; j < 4; j ++)
141 {
142 cout << puzzleGraph[i][j] << " ";
143 }
144 cout << puzzleGraph[i][4] << endl;
145 }
146 }
147 return 0;
148 }