#include <algorithm> #include <bits/stdc++.h> using namespace std; int n, m, siz; char t[507][507][2]; char start[507][507]; pair <int, int> mv[507][507][20]; int p2[20]; string s; int first_command, second_command, pos; char movefrompos[5][2]; ///przod, tyl map <pair <int, char>, int> poschange; map <char, int> mapa; void cycle(int x){ for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ t[j][i][0] = '.'; } } int mfw, a, b, a1, b1; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if(start[j][i] != '.'){ mfw = 0; a = j; b = i; // cout << "--------------\n" << j << ' ' << i << "\n---------------\n"; for(int k = 19; k >= 0; k--){ // cout << mv[j][i][k].first << ',' << mv[j][i][k].second << '\n'; if(mfw + p2[k] <= x){ mfw += p2[k]; a1 = mv[a][b][k].first; b1 = mv[a][b][k].second; a = a1; b = b1; // cout << a << ' ' << b << '\n'; } } t[a][b][0] = start[j][i]; } } } } void save(vector <pair <pair <int, int>, pair <int, int>>> moves, int nr){ // cout << "COPE" << nr << ' ' << moves.size() << '\n'; // for(int i = 1; i <= n; i++){ // for(int j = 1; j <= m; j++){ // mv[j][i][nr] = {0,0}; // } // } for(int i = 0; i < moves.size(); i++){ // cout << moves[i].first.first << ',' << moves[i].first.second << ' ' << moves[i].second.first << ',' << moves[i].second.second << '\n'; mv[moves[i].first.first][moves[i].first.second][nr] = {moves[i].second.first, moves[i].second.second}; } // for(int i = 1; i <= n; i++){ // for(int j = 1; j <= m; j++){ // cout << mv[j][i][nr].first << ',' << mv[j][i][nr].second << ' '; // } // cout << '\n'; // } if(nr == 4){ for(int j = 1; j <= m; j++){ for(int i = 1; i <= n; i++){ if(mv[j][i][1].first == 0) continue; for(int k = 2; k <= 4; k++){ // cout << "j=" << j << " i=" << i << '\n' << mv[j][i][1].first << ',' << mv[j][i][1].second << '\n'; mv[j][i][1] = mv[mv[j][i][1].first][mv[j][i][1].second][k]; } mv[j][i][0] = mv[j][i][1]; } } // for(int i = 1; i <= n; i++){ // for(int j = 1; j <= m; j++){ // cout << mv[j][i][0].first << ',' << mv[j][i][0].second << ' '; // } // cout << '\n'; // } for(int k = 1; k < 20; k++){ for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if(mv[j][i][0].first == 0) continue; mv[j][i][k] = mv[mv[j][i][k-1].first][mv[j][i][k-1].second][k-1]; } } } } } void move(char c, int nr){ vector <pair <char, int>> v; vector <pair <pair <int, int>, pair <int, int>>> moves; // cout << c << ' ' << nr << '\n'; if(c == 'G'){ for(int j = 1; j <= m; j++){ for(int i = 1; i <= n; i++){ if(t[j][i][0] != '.') v.push_back({t[j][i][0], i}); } for(int i = 0; i < v.size(); i++){ t[j][i+1][1] = v[i].first; moves.push_back({{j, v[i].second},{j, i+1}}); } v.clear(); } } else if(c == 'D'){ for(int j = 1; j <= m; j++){ for(int i = n; i >= 1; i--){ if(t[j][i][0] != '.') v.push_back({t[j][i][0], i}); } for(int i = 0; i < v.size(); i++){ t[j][n-i][1] = v[i].first; moves.push_back({{j, v[i].second},{j, n-i}}); } v.clear(); } } else if(c == 'L'){ for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if(t[j][i][0] != '.') v.push_back({t[j][i][0], j}); } for(int j = 0; j < v.size(); j++){ t[j+1][i][1] = v[j].first; moves.push_back({{v[j].second, i},{j+1, i}}); } v.clear(); } } else if(c == 'P'){ for(int i = 1; i <= n; i++){ for(int j = m; j >= 1; j--){ if(t[j][i][0] != '.') v.push_back({t[j][i][0], j}); } for(int j = 0; j < v.size(); j++){ t[m-j][i][1] = v[j].first; moves.push_back({{v[j].second, i},{m-j, i}}); } v.clear(); } } if(nr) save(moves, nr); for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ t[j][i][0] = t[j][i][1]; t[j][i][1] = '.'; } } v.clear(); moves.clear(); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for(int i = 1; i <= n; i++){ cin >> s; for(int j = 0; j < s.size(); j++){ t[j+1][i][0] = s[j]; t[j+1][i][1] = '.'; } } cin >> siz >> s; int sex = 1; mapa['D'] = 0; mapa['G'] = 0; mapa['P'] = 1; mapa['L'] = 1; while(mapa[s[first_command]] == mapa[s[second_command]] && second_command < siz-1){ if(mapa[s[second_command]] == mapa[s[first_command]]) first_command = second_command; second_command++; } move(s[first_command], 0); move(s[second_command], 0); for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ start[j][i] = t[j][i][0]; } } if(first_command == second_command){ for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ cout << t[j][i][0]; } cout << '\n'; } return 0; } if(s[first_command] == 'G' && s[second_command] == 'P' || s[first_command] == 'P' && s[second_command] == 'G') pos = 1; else if(s[first_command] == 'D' && s[second_command] == 'P' || s[first_command] == 'P' && s[second_command] == 'D') pos = 2; else if(s[first_command] == 'D' && s[second_command] == 'L' || s[first_command] == 'L' && s[second_command] == 'D') pos = 3; else if(s[first_command] == 'G' && s[second_command] == 'L' || s[first_command] == 'L' && s[second_command] == 'G') pos = 4; int recentpos = pos, moveby = 0; for(int i = second_command+1; i <= s.size(); i++){ if(recentpos == 1){ if(s[i] == 'D'){ recentpos = 2; moveby++; } else if(s[i] == 'L'){ recentpos = 4; moveby--; } } else if(recentpos == 2){ if(s[i] == 'G'){ recentpos = 1; moveby--; } else if(s[i] == 'L'){ recentpos = 3; moveby++; } } else if(recentpos == 3){ if(s[i] == 'P'){ recentpos = 2; moveby--; } else if(s[i] == 'G'){ recentpos = 4; moveby++; } } else{ if(s[i] == 'D'){ recentpos = 3; moveby--; } else if(s[i] == 'P'){ recentpos = 1; moveby++; } } } p2[0] = 1; for(int i = 1; i < 20; i++) p2[i] = p2[i-1]*2; movefrompos[1][0] = 'D'; movefrompos[1][1] = 'L'; movefrompos[2][0] = 'L'; movefrompos[2][1] = 'G'; movefrompos[3][0] = 'G'; movefrompos[3][1] = 'P'; movefrompos[4][0] = 'P'; movefrompos[4][1] = 'D'; poschange[{1,'D'}] = 2; poschange[{1,'L'}] = 4; poschange[{1,'P'}] = 1; poschange[{1,'G'}] = 1; poschange[{2,'G'}] = 1; poschange[{2,'L'}] = 3; poschange[{2,'P'}] = 2; poschange[{2,'D'}] = 2; poschange[{3,'G'}] = 4; poschange[{3,'P'}] = 2; poschange[{3,'L'}] = 3; poschange[{3,'D'}] = 3; poschange[{4,'D'}] = 3; poschange[{4,'P'}] = 1; poschange[{4,'L'}] = 4; poschange[{4,'G'}] = 4; recentpos = pos; // cout << "moveby=" << moveby << '\n'; if(moveby >= 0){ for(int i = 1; i <= 4; i++){ move(movefrompos[recentpos][0], i); recentpos = poschange[{recentpos, movefrompos[recentpos][0]}]; } cycle(moveby/4); for(int i = 1; i <= moveby%4; i++){ move(movefrompos[pos][0], 0); pos = poschange[{pos, movefrompos[pos][0]}]; } for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ cout << t[j][i][0]; } cout << '\n'; } } else{ for(int i = 1; i <= 4; i++){ move(movefrompos[recentpos][1], i); recentpos = poschange[{recentpos, movefrompos[recentpos][1]}]; } moveby = -moveby; cycle(moveby/4); // cout << "pos=" << pos << '\n'; for(int i = 1; i <= moveby%4; i++){ move(movefrompos[pos][1], 0); pos = poschange[{pos, movefrompos[pos][1]}]; // cout << "pos=" << pos << '\n'; } for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ cout << t[j][i][0]; } 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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | #include <algorithm> #include <bits/stdc++.h> using namespace std; int n, m, siz; char t[507][507][2]; char start[507][507]; pair <int, int> mv[507][507][20]; int p2[20]; string s; int first_command, second_command, pos; char movefrompos[5][2]; ///przod, tyl map <pair <int, char>, int> poschange; map <char, int> mapa; void cycle(int x){ for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ t[j][i][0] = '.'; } } int mfw, a, b, a1, b1; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if(start[j][i] != '.'){ mfw = 0; a = j; b = i; // cout << "--------------\n" << j << ' ' << i << "\n---------------\n"; for(int k = 19; k >= 0; k--){ // cout << mv[j][i][k].first << ',' << mv[j][i][k].second << '\n'; if(mfw + p2[k] <= x){ mfw += p2[k]; a1 = mv[a][b][k].first; b1 = mv[a][b][k].second; a = a1; b = b1; // cout << a << ' ' << b << '\n'; } } t[a][b][0] = start[j][i]; } } } } void save(vector <pair <pair <int, int>, pair <int, int>>> moves, int nr){ // cout << "COPE" << nr << ' ' << moves.size() << '\n'; // for(int i = 1; i <= n; i++){ // for(int j = 1; j <= m; j++){ // mv[j][i][nr] = {0,0}; // } // } for(int i = 0; i < moves.size(); i++){ // cout << moves[i].first.first << ',' << moves[i].first.second << ' ' << moves[i].second.first << ',' << moves[i].second.second << '\n'; mv[moves[i].first.first][moves[i].first.second][nr] = {moves[i].second.first, moves[i].second.second}; } // for(int i = 1; i <= n; i++){ // for(int j = 1; j <= m; j++){ // cout << mv[j][i][nr].first << ',' << mv[j][i][nr].second << ' '; // } // cout << '\n'; // } if(nr == 4){ for(int j = 1; j <= m; j++){ for(int i = 1; i <= n; i++){ if(mv[j][i][1].first == 0) continue; for(int k = 2; k <= 4; k++){ // cout << "j=" << j << " i=" << i << '\n' << mv[j][i][1].first << ',' << mv[j][i][1].second << '\n'; mv[j][i][1] = mv[mv[j][i][1].first][mv[j][i][1].second][k]; } mv[j][i][0] = mv[j][i][1]; } } // for(int i = 1; i <= n; i++){ // for(int j = 1; j <= m; j++){ // cout << mv[j][i][0].first << ',' << mv[j][i][0].second << ' '; // } // cout << '\n'; // } for(int k = 1; k < 20; k++){ for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if(mv[j][i][0].first == 0) continue; mv[j][i][k] = mv[mv[j][i][k-1].first][mv[j][i][k-1].second][k-1]; } } } } } void move(char c, int nr){ vector <pair <char, int>> v; vector <pair <pair <int, int>, pair <int, int>>> moves; // cout << c << ' ' << nr << '\n'; if(c == 'G'){ for(int j = 1; j <= m; j++){ for(int i = 1; i <= n; i++){ if(t[j][i][0] != '.') v.push_back({t[j][i][0], i}); } for(int i = 0; i < v.size(); i++){ t[j][i+1][1] = v[i].first; moves.push_back({{j, v[i].second},{j, i+1}}); } v.clear(); } } else if(c == 'D'){ for(int j = 1; j <= m; j++){ for(int i = n; i >= 1; i--){ if(t[j][i][0] != '.') v.push_back({t[j][i][0], i}); } for(int i = 0; i < v.size(); i++){ t[j][n-i][1] = v[i].first; moves.push_back({{j, v[i].second},{j, n-i}}); } v.clear(); } } else if(c == 'L'){ for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if(t[j][i][0] != '.') v.push_back({t[j][i][0], j}); } for(int j = 0; j < v.size(); j++){ t[j+1][i][1] = v[j].first; moves.push_back({{v[j].second, i},{j+1, i}}); } v.clear(); } } else if(c == 'P'){ for(int i = 1; i <= n; i++){ for(int j = m; j >= 1; j--){ if(t[j][i][0] != '.') v.push_back({t[j][i][0], j}); } for(int j = 0; j < v.size(); j++){ t[m-j][i][1] = v[j].first; moves.push_back({{v[j].second, i},{m-j, i}}); } v.clear(); } } if(nr) save(moves, nr); for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ t[j][i][0] = t[j][i][1]; t[j][i][1] = '.'; } } v.clear(); moves.clear(); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for(int i = 1; i <= n; i++){ cin >> s; for(int j = 0; j < s.size(); j++){ t[j+1][i][0] = s[j]; t[j+1][i][1] = '.'; } } cin >> siz >> s; int sex = 1; mapa['D'] = 0; mapa['G'] = 0; mapa['P'] = 1; mapa['L'] = 1; while(mapa[s[first_command]] == mapa[s[second_command]] && second_command < siz-1){ if(mapa[s[second_command]] == mapa[s[first_command]]) first_command = second_command; second_command++; } move(s[first_command], 0); move(s[second_command], 0); for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ start[j][i] = t[j][i][0]; } } if(first_command == second_command){ for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ cout << t[j][i][0]; } cout << '\n'; } return 0; } if(s[first_command] == 'G' && s[second_command] == 'P' || s[first_command] == 'P' && s[second_command] == 'G') pos = 1; else if(s[first_command] == 'D' && s[second_command] == 'P' || s[first_command] == 'P' && s[second_command] == 'D') pos = 2; else if(s[first_command] == 'D' && s[second_command] == 'L' || s[first_command] == 'L' && s[second_command] == 'D') pos = 3; else if(s[first_command] == 'G' && s[second_command] == 'L' || s[first_command] == 'L' && s[second_command] == 'G') pos = 4; int recentpos = pos, moveby = 0; for(int i = second_command+1; i <= s.size(); i++){ if(recentpos == 1){ if(s[i] == 'D'){ recentpos = 2; moveby++; } else if(s[i] == 'L'){ recentpos = 4; moveby--; } } else if(recentpos == 2){ if(s[i] == 'G'){ recentpos = 1; moveby--; } else if(s[i] == 'L'){ recentpos = 3; moveby++; } } else if(recentpos == 3){ if(s[i] == 'P'){ recentpos = 2; moveby--; } else if(s[i] == 'G'){ recentpos = 4; moveby++; } } else{ if(s[i] == 'D'){ recentpos = 3; moveby--; } else if(s[i] == 'P'){ recentpos = 1; moveby++; } } } p2[0] = 1; for(int i = 1; i < 20; i++) p2[i] = p2[i-1]*2; movefrompos[1][0] = 'D'; movefrompos[1][1] = 'L'; movefrompos[2][0] = 'L'; movefrompos[2][1] = 'G'; movefrompos[3][0] = 'G'; movefrompos[3][1] = 'P'; movefrompos[4][0] = 'P'; movefrompos[4][1] = 'D'; poschange[{1,'D'}] = 2; poschange[{1,'L'}] = 4; poschange[{1,'P'}] = 1; poschange[{1,'G'}] = 1; poschange[{2,'G'}] = 1; poschange[{2,'L'}] = 3; poschange[{2,'P'}] = 2; poschange[{2,'D'}] = 2; poschange[{3,'G'}] = 4; poschange[{3,'P'}] = 2; poschange[{3,'L'}] = 3; poschange[{3,'D'}] = 3; poschange[{4,'D'}] = 3; poschange[{4,'P'}] = 1; poschange[{4,'L'}] = 4; poschange[{4,'G'}] = 4; recentpos = pos; // cout << "moveby=" << moveby << '\n'; if(moveby >= 0){ for(int i = 1; i <= 4; i++){ move(movefrompos[recentpos][0], i); recentpos = poschange[{recentpos, movefrompos[recentpos][0]}]; } cycle(moveby/4); for(int i = 1; i <= moveby%4; i++){ move(movefrompos[pos][0], 0); pos = poschange[{pos, movefrompos[pos][0]}]; } for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ cout << t[j][i][0]; } cout << '\n'; } } else{ for(int i = 1; i <= 4; i++){ move(movefrompos[recentpos][1], i); recentpos = poschange[{recentpos, movefrompos[recentpos][1]}]; } moveby = -moveby; cycle(moveby/4); // cout << "pos=" << pos << '\n'; for(int i = 1; i <= moveby%4; i++){ move(movefrompos[pos][1], 0); pos = poschange[{pos, movefrompos[pos][1]}]; // cout << "pos=" << pos << '\n'; } for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ cout << t[j][i][0]; } cout << '\n'; } } return 0; } |