#include <iostream> #include <vector> #include <stack> #include <bitset> using namespace std; constexpr int N = 507; vector <char> seq; char p1, p2;// p1 -> up/ down; p2 -> left/ right stack <char> s; string str; pair <int, int> f1[N][N], f2[N][N], f3[N][N], w1[N * N], w2[N * N]; pair <pair <int, int>, pair <int, int>> lim[5]; int tab[N][N], T[N][N], d = 2; inline void change_p(char x) { if(x == 'G' || x == 'D') p1 = x; if(x == 'L' || x == 'P') p2 = x; } inline int solve_beginning(int k) { int i = 0; while((!p1 || !p2) && i < k) { change_p(str[i]); i++; } if(!p1 || !p2) return -1; return i; } inline bool not_same(char x) { if(x == p1 || x == p2) return 0; return 1; } inline bool complementary(char a, char b) { if(a > b) swap(a, b); if(a == 'D' && b == 'G') return 1; if(a == 'L' && b == 'P') return 1; return 0; } inline bool not_symetric(char x) {//involves erasing some elements from the top if(s.empty()) return 1; if(!complementary(s.top(), x)) return 1; s.pop(); d--; if(d < 2) return 1; int a = s.top(); s.pop(); if(s.top() == x) { change_p(s.top()); s.push(a); return 0; } s.push(a); return 1; } inline bool change_seq(int k) {//edit beginning of the sequence cin >> str; int b = solve_beginning(k); if(b == -1) { seq.push_back(str.back()); return 0; } s.push(str[b - 2]); s.push(str[b - 1]); for(int i = b; i < k; i++) if(not_same(str[i]) && not_symetric(str[i])) { s.push(str[i]); change_p(str[i]); d++; } seq.resize(d); //seq[0] = str[b - 1]; //seq[1] = str[b]; for(int i = seq.size() - 1; i >= 0; i--) { seq[i] = s.top(); s.pop(); } return 1; } inline void horizonal(int a1, int a2, int r1, int b1, int b2, int r2) { for(int j = a1; r1 * j <= r1 * a2; j += r1) { int p = b1; for(int i = b1; r2 * i <= r2 * b2; i += r2) if(tab[i][j] != '.') { while(tab[p][j] != '.' && p != i) p++; tab[p][j] = tab[i][j]; if(p != i) tab[i][j] = '.'; p += r2; } } } inline void vertical(int a1, int a2, int r1, int b1, int b2, int r2) { for(int i = a1; r1 * i <= r1 * a2; i += r1) { int p = b1; for(int j = b1; r2 * j <= r2 * b2; j += r2) if(tab[i][j] != '.') { while(tab[i][p] != '.' && p != j) p++; tab[i][p] = tab[i][j]; if(p != j) tab[i][j] = '.'; p += r2; } } } inline void make_a_move(int x) { int c; if(x == 'G') c = 0; if(x == 'D') c = 1; if(x == 'L') c = 2; if(x == 'P') c = 3; int a1 = lim[c].first.first, a2 = lim[c].first.second, b1 = lim[c].second.first, b2 = lim[c].second.second; int r1 = 1, r2 = 1; if(a1 > a2) r1 = -1; if(b1 > b2) r2 = -1; if(x == 'G' || x == 'D') horizonal(a1, a2, r1, b1, b2, r2); else vertical(a1, a2, r1, b1, b2, r2); } inline void multiply(pair <int, int> t1[N][N], pair <int, int> t2[N][N], int n, int m) { for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) T[i][j] = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { T[t2[i][j].first][t2[i][j].second]++; if(T[t2[i][j].first][t2[i][j].second] > 1) cout << "XD\n"; } for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) f3[i][j] = t2[t1[i][j].first][t1[i][j].second]; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) t1[i][j] = f3[i][j]; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) T[i][j] = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { T[t1[i][j].first][t1[i][j].second]++; if(T[t1[i][j].first][t1[i][j].second] > 1) cout << "XD\n"; } } inline void fast_powering(int pot, int n, int m) { while(pot) { if(pot & 1) multiply(f2, f1, n, m); multiply(f1, f1, n, m); pot >>= 1; } } inline void find_locations(int n, int m, int k) { int keyd = '.', key = keyd + 1; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { if(tab[i][j] == '.') T[i][j] = keyd; else { T[i][j] = key; w1[key++] = {i, j}; } } for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) swap(tab[i][j], T[i][j]); for(int i = 2; i <= 5; i++) make_a_move(seq[i]); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) swap(tab[i][j], T[i][j]); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) if(T[i][j] > keyd) w2[T[i][j]] = {i, j}; for(int i = keyd + 1; i < key; i++) f1[w1[i].first][w1[i].second] = {w2[i].first, w2[i].second}; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { f2[i][j] = {i, j}; if(!f1[i][j].first && !f1[i][j].second) f1[i][j] = {i, j}; } fast_powering((seq.size() - 2) / 4, n, m); } inline void adjust_locations(int n, int m) { for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) T[i][j] = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) T[f2[i][j].first][f2[i][j].second] = tab[i][j]; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { if(T[i][j]) tab[i][j] = T[i][j]; else tab[i][j] = '.'; } } inline void solve_seq(int n, int m, int k) { if(!change_seq(k)) {// we don't go to any corner make_a_move(seq[0]); return; } if(seq.size() <= 10) { for(int i = 0; i < seq.size(); i++) make_a_move(seq[i]); } else { make_a_move(seq[0]); make_a_move(seq[1]); find_locations(n, m, seq.size()); adjust_locations(n, m); for(int i = seq.size() - (seq.size() - 2) % 4; i < (int)seq.size(); i++) make_a_move(seq[i]); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; lim[0] = {{1, m}, {1, n}}; lim[1] = {{1, m}, {n, 1}}; lim[2] = {{1, n}, {1, m}}; lim[3] = {{1, n}, {m, 1}}; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { char a; cin >> a; tab[i][j] = a; } int k; cin >> k; solve_seq(n, m, k); /*for(int i = 0; i < seq.size(); i++) cout << seq[i]; cout << "\n"; //return 0; /*for(int t = 0; t < (int)seq.size(); t++) { char o = seq[t]; //cin >> o; cout << t + 1 << "\n"; make_a_move(o); for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) cout << tab[i][j]; cout << "\n"; } cout << "\n"; }*/ for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) cout << (char)tab[i][j]; cout << "\n"; } 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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | #include <iostream> #include <vector> #include <stack> #include <bitset> using namespace std; constexpr int N = 507; vector <char> seq; char p1, p2;// p1 -> up/ down; p2 -> left/ right stack <char> s; string str; pair <int, int> f1[N][N], f2[N][N], f3[N][N], w1[N * N], w2[N * N]; pair <pair <int, int>, pair <int, int>> lim[5]; int tab[N][N], T[N][N], d = 2; inline void change_p(char x) { if(x == 'G' || x == 'D') p1 = x; if(x == 'L' || x == 'P') p2 = x; } inline int solve_beginning(int k) { int i = 0; while((!p1 || !p2) && i < k) { change_p(str[i]); i++; } if(!p1 || !p2) return -1; return i; } inline bool not_same(char x) { if(x == p1 || x == p2) return 0; return 1; } inline bool complementary(char a, char b) { if(a > b) swap(a, b); if(a == 'D' && b == 'G') return 1; if(a == 'L' && b == 'P') return 1; return 0; } inline bool not_symetric(char x) {//involves erasing some elements from the top if(s.empty()) return 1; if(!complementary(s.top(), x)) return 1; s.pop(); d--; if(d < 2) return 1; int a = s.top(); s.pop(); if(s.top() == x) { change_p(s.top()); s.push(a); return 0; } s.push(a); return 1; } inline bool change_seq(int k) {//edit beginning of the sequence cin >> str; int b = solve_beginning(k); if(b == -1) { seq.push_back(str.back()); return 0; } s.push(str[b - 2]); s.push(str[b - 1]); for(int i = b; i < k; i++) if(not_same(str[i]) && not_symetric(str[i])) { s.push(str[i]); change_p(str[i]); d++; } seq.resize(d); //seq[0] = str[b - 1]; //seq[1] = str[b]; for(int i = seq.size() - 1; i >= 0; i--) { seq[i] = s.top(); s.pop(); } return 1; } inline void horizonal(int a1, int a2, int r1, int b1, int b2, int r2) { for(int j = a1; r1 * j <= r1 * a2; j += r1) { int p = b1; for(int i = b1; r2 * i <= r2 * b2; i += r2) if(tab[i][j] != '.') { while(tab[p][j] != '.' && p != i) p++; tab[p][j] = tab[i][j]; if(p != i) tab[i][j] = '.'; p += r2; } } } inline void vertical(int a1, int a2, int r1, int b1, int b2, int r2) { for(int i = a1; r1 * i <= r1 * a2; i += r1) { int p = b1; for(int j = b1; r2 * j <= r2 * b2; j += r2) if(tab[i][j] != '.') { while(tab[i][p] != '.' && p != j) p++; tab[i][p] = tab[i][j]; if(p != j) tab[i][j] = '.'; p += r2; } } } inline void make_a_move(int x) { int c; if(x == 'G') c = 0; if(x == 'D') c = 1; if(x == 'L') c = 2; if(x == 'P') c = 3; int a1 = lim[c].first.first, a2 = lim[c].first.second, b1 = lim[c].second.first, b2 = lim[c].second.second; int r1 = 1, r2 = 1; if(a1 > a2) r1 = -1; if(b1 > b2) r2 = -1; if(x == 'G' || x == 'D') horizonal(a1, a2, r1, b1, b2, r2); else vertical(a1, a2, r1, b1, b2, r2); } inline void multiply(pair <int, int> t1[N][N], pair <int, int> t2[N][N], int n, int m) { for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) T[i][j] = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { T[t2[i][j].first][t2[i][j].second]++; if(T[t2[i][j].first][t2[i][j].second] > 1) cout << "XD\n"; } for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) f3[i][j] = t2[t1[i][j].first][t1[i][j].second]; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) t1[i][j] = f3[i][j]; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) T[i][j] = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { T[t1[i][j].first][t1[i][j].second]++; if(T[t1[i][j].first][t1[i][j].second] > 1) cout << "XD\n"; } } inline void fast_powering(int pot, int n, int m) { while(pot) { if(pot & 1) multiply(f2, f1, n, m); multiply(f1, f1, n, m); pot >>= 1; } } inline void find_locations(int n, int m, int k) { int keyd = '.', key = keyd + 1; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { if(tab[i][j] == '.') T[i][j] = keyd; else { T[i][j] = key; w1[key++] = {i, j}; } } for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) swap(tab[i][j], T[i][j]); for(int i = 2; i <= 5; i++) make_a_move(seq[i]); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) swap(tab[i][j], T[i][j]); for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) if(T[i][j] > keyd) w2[T[i][j]] = {i, j}; for(int i = keyd + 1; i < key; i++) f1[w1[i].first][w1[i].second] = {w2[i].first, w2[i].second}; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { f2[i][j] = {i, j}; if(!f1[i][j].first && !f1[i][j].second) f1[i][j] = {i, j}; } fast_powering((seq.size() - 2) / 4, n, m); } inline void adjust_locations(int n, int m) { for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) T[i][j] = 0; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) T[f2[i][j].first][f2[i][j].second] = tab[i][j]; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { if(T[i][j]) tab[i][j] = T[i][j]; else tab[i][j] = '.'; } } inline void solve_seq(int n, int m, int k) { if(!change_seq(k)) {// we don't go to any corner make_a_move(seq[0]); return; } if(seq.size() <= 10) { for(int i = 0; i < seq.size(); i++) make_a_move(seq[i]); } else { make_a_move(seq[0]); make_a_move(seq[1]); find_locations(n, m, seq.size()); adjust_locations(n, m); for(int i = seq.size() - (seq.size() - 2) % 4; i < (int)seq.size(); i++) make_a_move(seq[i]); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; lim[0] = {{1, m}, {1, n}}; lim[1] = {{1, m}, {n, 1}}; lim[2] = {{1, n}, {1, m}}; lim[3] = {{1, n}, {m, 1}}; for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) { char a; cin >> a; tab[i][j] = a; } int k; cin >> k; solve_seq(n, m, k); /*for(int i = 0; i < seq.size(); i++) cout << seq[i]; cout << "\n"; //return 0; /*for(int t = 0; t < (int)seq.size(); t++) { char o = seq[t]; //cin >> o; cout << t + 1 << "\n"; make_a_move(o); for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) cout << tab[i][j]; cout << "\n"; } cout << "\n"; }*/ for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) cout << (char)tab[i][j]; cout << "\n"; } return 0; } |