#include <bits/stdc++.h> #define fi first #define sc second #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define forr(i,k,p) for(int i=(k);i>=(p);--i) #define ALL(v) begin(v), end(v) #define RET(smth) return void(cout<<smth<<'\n'); #define SIZ(v) (int)v.size() using namespace std; using pii = pair<int,int>; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif const int MM = 510; int T[MM][MM]; int col[MM*MM]; int n,m; void print() { cerr << "#################################\n"; for(int i=0;i<n;i++) for(int j=0;j<m;j++) cerr << T[i][j] << " \n"[j==m-1]; cerr << "#################################\n"; } void print_ans() { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) cout << (T[i][j] ? col[T[i][j]] == 1 ? 'B' : 'C' : '.'); cout << '\n'; } } void up() { for(int j=0;j<m;j++) { int cnt = 0; for(int i=0;i<n;i++) if(T[i][j]) swap(T[cnt++][j],T[i][j]); } } void down() { for(int j=0;j<m;j++) { int cnt = n-1; for(int i=n-1;i>=0;i--) if(T[i][j]) swap(T[cnt--][j],T[i][j]); } } void left() { for(int i=0;i<n;i++) { int cnt = 0; for(int j=0;j<m;j++) if(T[i][j]) swap(T[i][cnt++],T[i][j]); } } void right() { for(int i=0;i<n;i++) { int cnt = m-1; for(int j=m-1;j>=0;j--) if(T[i][j]) swap(T[i][cnt--],T[i][j]); } } void action(char z) { if(z == 'L') left(); if(z == 'P') right(); if(z == 'G') up(); if(z == 'D') down(); } int main() { #ifndef DEBUG ios_base::sync_with_stdio(0); cin.tie(0); #endif cin >> n >> m; int cnt = 1; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { char c; cin >> c; if(c != '.') { if(c == 'B') col[cnt] = 1; if(c == 'C') col[cnt] = 2; T[i][j] = cnt++; } } int k; string S; cin >> k >> S; auto is_opp = [](char x, char y) { if(x > y) swap(x,y); if(x == 'L' && y == 'P') return true; if(x == 'D' && y == 'G') return true; return false; }; int ind = -1; char pie = 0; for(int i=0;i<k;i++) { char c = S[i]; if(pie == c) continue; if(!pie) { pie = c; continue; } else { if(is_opp(pie,c)) { pie = c; continue; } else { action(pie); action(c); ind = i; break; } } } if(ind == -1) { action(pie); // cerr << "DONE -1\n"; print_ans(); return 0; } const auto is_hor = [](char x) { return x == 'L' || x == 'P'; }; char hor = pie; char ver = S[ind]; if(!is_hor(hor)) swap(hor,ver); string actions; // cerr << "CORNER :" << hor << ' ' << ver << '\n'; char chor = hor, cver = ver; const auto update_pos = [&chor, &cver](int c) { if(c == 'L' || c == 'P') chor = c; else cver = c; }; for(int i=ind+1;i<k;i++) { char c = S[i]; if(c == chor || c == cver) continue; if(actions.empty()) { actions.push_back(c); update_pos(c); continue; } if(is_opp(actions.back(),c)) { actions.pop_back(); update_pos(c); continue; } actions.push_back(c); update_pos(c); } // cerr << "REST ACTIONS:\n" << actions << '\n'; if(actions.size() < 4) { for(auto v:actions) { action(v); } // print(); // cerr << "DONE small\n"; print_ans(); return 0; } vector<pair<int,int>> pos(cnt); vector<int> per(cnt); // cerr << "BEFORE\n"; // print(); for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(T[i][j]) pos[T[i][j]] = {i,j}; for(int i=0;i<4;i++) action(actions[i]); // cerr << "AFTER\n"; // print(); for(int i=1;i<cnt;i++) { auto [x,y] = pos[i]; per[i] = T[x][y]; } // cerr << "PERMUTATION:\n"; // for(int i=1;i<cnt;i++) cerr << per[i] << ' '; // cerr << '\n'; vector<int> num(cnt), cycle_ind(cnt); vector<bool> vis(cnt, false); vector<vector<int>> cycles; int cycle_cnt = 0; for(int i=1;i<cnt;i++) if(!vis[i]){ vector<int> C; C.push_back(i); num[i] = cycle_cnt; int cur = i; int l = 0; cycle_ind[i] = l; vis[cur] = true; while(!vis[per[cur]]) { cur = per[cur]; l++; cycle_ind[cur] = l; num[cur] = cycle_cnt; C.push_back(cur); vis[cur] = true; } cycles.push_back(move(C)); cycle_cnt++; } // for(int i=1;i<cnt;i++) cerr << num[i] << ' '; // cerr << '\n'; const auto apply = [&](int x, int N) { int l = cycles[num[x]].size(); return cycles[num[x]][(cycle_ind[x] + N) % l]; }; int num_of_cycles = (actions.size() / 4) - 1; for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(T[i][j]) { T[i][j] = apply(T[i][j], num_of_cycles); } // cerr << "AFTER CYCLES\n"; // print(); int rest = actions.size() % 4; for(int i=0;i<rest;i++) { char c = actions[i]; action(c); } // cerr << "AFTER EVERYTHING"; // print(); print_ans(); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | #include <bits/stdc++.h> #define fi first #define sc second #define forn(i,p,k) for(int i=(p);i<=(k);++i) #define forr(i,k,p) for(int i=(k);i>=(p);--i) #define ALL(v) begin(v), end(v) #define RET(smth) return void(cout<<smth<<'\n'); #define SIZ(v) (int)v.size() using namespace std; using pii = pair<int,int>; using ll = long long; #ifdef DEBUG #include "debug.h" #else #define debug(...) ; #endif const int MM = 510; int T[MM][MM]; int col[MM*MM]; int n,m; void print() { cerr << "#################################\n"; for(int i=0;i<n;i++) for(int j=0;j<m;j++) cerr << T[i][j] << " \n"[j==m-1]; cerr << "#################################\n"; } void print_ans() { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) cout << (T[i][j] ? col[T[i][j]] == 1 ? 'B' : 'C' : '.'); cout << '\n'; } } void up() { for(int j=0;j<m;j++) { int cnt = 0; for(int i=0;i<n;i++) if(T[i][j]) swap(T[cnt++][j],T[i][j]); } } void down() { for(int j=0;j<m;j++) { int cnt = n-1; for(int i=n-1;i>=0;i--) if(T[i][j]) swap(T[cnt--][j],T[i][j]); } } void left() { for(int i=0;i<n;i++) { int cnt = 0; for(int j=0;j<m;j++) if(T[i][j]) swap(T[i][cnt++],T[i][j]); } } void right() { for(int i=0;i<n;i++) { int cnt = m-1; for(int j=m-1;j>=0;j--) if(T[i][j]) swap(T[i][cnt--],T[i][j]); } } void action(char z) { if(z == 'L') left(); if(z == 'P') right(); if(z == 'G') up(); if(z == 'D') down(); } int main() { #ifndef DEBUG ios_base::sync_with_stdio(0); cin.tie(0); #endif cin >> n >> m; int cnt = 1; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { char c; cin >> c; if(c != '.') { if(c == 'B') col[cnt] = 1; if(c == 'C') col[cnt] = 2; T[i][j] = cnt++; } } int k; string S; cin >> k >> S; auto is_opp = [](char x, char y) { if(x > y) swap(x,y); if(x == 'L' && y == 'P') return true; if(x == 'D' && y == 'G') return true; return false; }; int ind = -1; char pie = 0; for(int i=0;i<k;i++) { char c = S[i]; if(pie == c) continue; if(!pie) { pie = c; continue; } else { if(is_opp(pie,c)) { pie = c; continue; } else { action(pie); action(c); ind = i; break; } } } if(ind == -1) { action(pie); // cerr << "DONE -1\n"; print_ans(); return 0; } const auto is_hor = [](char x) { return x == 'L' || x == 'P'; }; char hor = pie; char ver = S[ind]; if(!is_hor(hor)) swap(hor,ver); string actions; // cerr << "CORNER :" << hor << ' ' << ver << '\n'; char chor = hor, cver = ver; const auto update_pos = [&chor, &cver](int c) { if(c == 'L' || c == 'P') chor = c; else cver = c; }; for(int i=ind+1;i<k;i++) { char c = S[i]; if(c == chor || c == cver) continue; if(actions.empty()) { actions.push_back(c); update_pos(c); continue; } if(is_opp(actions.back(),c)) { actions.pop_back(); update_pos(c); continue; } actions.push_back(c); update_pos(c); } // cerr << "REST ACTIONS:\n" << actions << '\n'; if(actions.size() < 4) { for(auto v:actions) { action(v); } // print(); // cerr << "DONE small\n"; print_ans(); return 0; } vector<pair<int,int>> pos(cnt); vector<int> per(cnt); // cerr << "BEFORE\n"; // print(); for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(T[i][j]) pos[T[i][j]] = {i,j}; for(int i=0;i<4;i++) action(actions[i]); // cerr << "AFTER\n"; // print(); for(int i=1;i<cnt;i++) { auto [x,y] = pos[i]; per[i] = T[x][y]; } // cerr << "PERMUTATION:\n"; // for(int i=1;i<cnt;i++) cerr << per[i] << ' '; // cerr << '\n'; vector<int> num(cnt), cycle_ind(cnt); vector<bool> vis(cnt, false); vector<vector<int>> cycles; int cycle_cnt = 0; for(int i=1;i<cnt;i++) if(!vis[i]){ vector<int> C; C.push_back(i); num[i] = cycle_cnt; int cur = i; int l = 0; cycle_ind[i] = l; vis[cur] = true; while(!vis[per[cur]]) { cur = per[cur]; l++; cycle_ind[cur] = l; num[cur] = cycle_cnt; C.push_back(cur); vis[cur] = true; } cycles.push_back(move(C)); cycle_cnt++; } // for(int i=1;i<cnt;i++) cerr << num[i] << ' '; // cerr << '\n'; const auto apply = [&](int x, int N) { int l = cycles[num[x]].size(); return cycles[num[x]][(cycle_ind[x] + N) % l]; }; int num_of_cycles = (actions.size() / 4) - 1; for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(T[i][j]) { T[i][j] = apply(T[i][j], num_of_cycles); } // cerr << "AFTER CYCLES\n"; // print(); int rest = actions.size() % 4; for(int i=0;i<rest;i++) { char c = actions[i]; action(c); } // cerr << "AFTER EVERYTHING"; // print(); print_ans(); return 0; } |