#include <iostream> #include <deque> using namespace std; enum class Edge { UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT }; enum class Direction { UP, DOWN, LEFT, RIGHT }; enum class Rotation { CLOCKWISE, COUNTER }; Direction get_dir(char c) { switch(c) { case 'G': return Direction::UP; case 'D': return Direction::DOWN; case 'L': return Direction::LEFT; default : return Direction::RIGHT; } } bool is_edge(Direction dir1, Direction dir2) { if (dir1 == Direction::UP || dir1 == Direction::DOWN) return dir2 == Direction::LEFT || dir2 == Direction::RIGHT; else return dir2 == Direction::UP || dir2 == Direction::DOWN; } Edge get_edge(Direction dir1, Direction dir2) { switch(dir1) { case Direction::UP: if (dir2 == Direction::LEFT) return Edge::UP_LEFT; else return Edge::UP_RIGHT; case Direction::DOWN: if (dir2 == Direction::LEFT) return Edge::DOWN_LEFT; else return Edge::DOWN_RIGHT; case Direction::LEFT: if (dir2 == Direction::UP) return Edge::UP_LEFT; else return Edge::DOWN_LEFT; default: if (dir2 == Direction::UP) return Edge::UP_RIGHT; else return Edge::DOWN_RIGHT; } } Rotation get_rotation(Edge edge, char c) { switch(edge) { case Edge::UP_RIGHT: if (c == 'D') return Rotation::CLOCKWISE; else return Rotation::COUNTER; case Edge::UP_LEFT: if (c == 'D') return Rotation::COUNTER; else return Rotation::CLOCKWISE; case Edge::DOWN_LEFT: if (c == 'G') return Rotation::CLOCKWISE; else return Rotation::COUNTER; default: if (c == 'G') return Rotation::COUNTER; else return Rotation::CLOCKWISE; } } Edge move_edge(char c, Edge edge) { switch(edge) { case Edge::UP_LEFT: if (c == 'P') return Edge::UP_RIGHT; else if (c == 'D') return Edge::DOWN_LEFT; else return Edge::UP_LEFT; case Edge::UP_RIGHT: if (c == 'L') return Edge::UP_LEFT; else if (c == 'D') return Edge::DOWN_RIGHT; else return Edge::UP_RIGHT; case Edge::DOWN_LEFT: if (c == 'P') return Edge::DOWN_RIGHT; else if (c == 'G') return Edge::UP_LEFT; else return Edge::DOWN_LEFT; default: if (c == 'L') return Edge::DOWN_LEFT; else if (c == 'G') return Edge::UP_RIGHT; else return Edge::DOWN_RIGHT; } } void up(char *board[], int n, int m) { for (int j = 0; j < m; j++) { int first_empty = 0; for (int i = 0; i < n; i++) if (board[i][j] != '.') board[first_empty++][j] = board[i][j]; for (int i = first_empty; i < n; i++) board[i][j] = '.'; } } void down(char *board[], int n, int m) { for (int j = 0; j < m; j++) { int first_empty = n-1; for (int i = n-1; i >= 0; i--) if (board[i][j] != '.') board[first_empty--][j] = board[i][j]; for (int i = first_empty; i >= 0; i--) board[i][j] = '.'; } } void left(char *board[], int n, int m) { for (int i = 0; i < n; i++) { int first_empty = 0; for (int j = 0; j < m; j++) if (board[i][j] != '.') board[i][first_empty++] = board[i][j]; for (int j = first_empty; j < m; j++) board[i][j] = '.'; } } void right(char *board[], int n, int m) { for (int i = 0; i < n; i++) { int first_empty = m-1; for (int j = m-1; j >= 0; j--) if (board[i][j] != '.') board[i][first_empty--] = board[i][j]; for (int j = first_empty; j >= 0; j--) board[i][j] = '.'; } } void print_board(char *board[], int n, int m) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cout << board[i][j]; } cout << "\n"; } } void print_board_int(int *board[], int n, int m) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cout << board[i][j] << "\t"; } cout << "\n"; } } void move(char *board[], int n, int m, Direction dir) { switch(dir) { case Direction::UP: up(board, n, m); break; case Direction::DOWN: down(board, n, m); break; case Direction::LEFT: left(board, n, m); break; default: right(board, n, m); break; } } void start_position(char *board[], int n, int m, Direction dir1, Direction dir2) { move(board, n, m, dir1); move(board, n, m, dir2); } void get_permutation_board(int *permutation_board[], char *board[], int n, int m) { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (board[i][j] != '.') permutation_board[i][j] = i*m+j; else permutation_board[i][j] = -1; } void up_int(int *board[], int n, int m) { for (int j = 0; j < m; j++) { int first_empty = 0; for (int i = 0; i < n; i++) if (board[i][j] != -1) board[first_empty++][j] = board[i][j]; for (int i = first_empty; i < n; i++) board[i][j] = -1; } } void down_int(int *board[], int n, int m) { for (int j = 0; j < m; j++) { int first_empty = n-1; for (int i = n-1; i >= 0; i--) if (board[i][j] != -1) board[first_empty--][j] = board[i][j]; for (int i = first_empty; i >= 0; i--) board[i][j] = -1; } } void left_int(int *board[], int n, int m) { for (int i = 0; i < n; i++) { int first_empty = 0; for (int j = 0; j < m; j++) if (board[i][j] != -1) board[i][first_empty++] = board[i][j]; for (int j = first_empty; j < m; j++) board[i][j] = -1; } } void right_int(int *board[], int n, int m) { for (int i = 0; i < n; i++) { int first_empty = m-1; for (int j = m-1; j >= 0; j--) if (board[i][j] != -1) board[i][first_empty--] = board[i][j]; for (int j = first_empty; j >= 0; j--) board[i][j] = -1; } } void rotate(int *board[], int n, int m, Edge edge, Rotation rotation) { switch (edge) { case Edge::UP_RIGHT: if (rotation == Rotation::CLOCKWISE) { down_int(board, n, m); left_int(board, n, m); up_int(board, n, m); right_int(board, n, m); } else { left_int(board, n, m); down_int(board, n, m); right_int(board, n, m); up_int(board, n, m); } break; case Edge::DOWN_RIGHT: if (rotation == Rotation::CLOCKWISE) { left_int(board, n, m); up_int(board, n, m); right_int(board, n, m); down_int(board, n, m); } else { up_int(board, n, m); left_int(board, n, m); down_int(board, n, m); right_int(board, n, m); } break; case Edge::DOWN_LEFT: if (rotation == Rotation::CLOCKWISE) { up_int(board, n, m); right_int(board, n, m); down_int(board, n, m); left_int(board, n, m); } else { right_int(board, n, m); up_int(board, n, m); left_int(board, n, m); down_int(board, n, m); } break; default: if (rotation == Rotation::CLOCKWISE) { right_int(board, n, m); down_int(board, n, m); left_int(board, n, m); up_int(board, n, m); } else { down_int(board, n, m); right_int(board, n, m); up_int(board, n, m); left_int(board, n, m); } break; } } void apply_permutation(char *board[], int *permutation[], int n, int m) { char *tmp[n]; for (int i = 0; i < n; i++) tmp[i] = new char[m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) tmp[i][j] = board[i][j]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int num = permutation[i][j]; if (num != -1) board[i][j] = tmp[num/m][num%m]; } } } void power_permutation(int *permutation[], int n, int m) { int *tmp[n]; for (int i = 0; i < n; i++) tmp[i] = new int[m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) tmp[i][j] = permutation[i][j]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int num = tmp[i][j]; if (num != -1) permutation[i][j] = tmp[num/m][num%m]; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; char *board[n]; for (int i = 0; i < n; i++) board[i] = new char[m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { cin >> board[i][j]; } int k; cin >> k; char moves[k]; for (int i = 0; i < k; i++) cin >> moves[i]; Direction dir1, dir2 = get_dir(moves[0]); int i = 1; bool end = i >= k; while (!end) { dir1 = dir2; dir2 = get_dir(moves[i++]); end = i >= k || is_edge(dir1, dir2); } if (!is_edge(dir1, dir2)) { move(board, n, m, dir2); print_board(board, n, m); return 0; } start_position(board, n, m, dir1, dir2); Edge edge, start_edge = get_edge(dir1, dir2); edge = start_edge; deque<int> compressed; for (; i < k; i++) { if (edge == move_edge(moves[i], edge)) continue; if (!compressed.empty()) { if ((moves[i] == 'L' && compressed.back() == 'P') || (moves[i] == 'G' && compressed.back() == 'D') || (moves[i] == 'P' && compressed.back() == 'L') || (moves[i] == 'D' && compressed.back() == 'G')) { edge = move_edge(moves[i], edge); compressed.pop_back(); continue; } } compressed.push_back(moves[i]); edge = move_edge(moves[i], edge); } Rotation rotation = get_rotation(start_edge, compressed.front()); int *permutation_board[n]; for (int i = 0; i < n; i++) permutation_board[i] = new int[m]; get_permutation_board(permutation_board, board, n, m); rotate(permutation_board, n, m, start_edge, rotation); int l = compressed.size()/4; while (l > 0) { if (l % 2 == 1) apply_permutation(board, permutation_board, n, m); l /= 2; power_permutation(permutation_board, n, m); } while (compressed.size() >= 4) { for (int i = 0; i < 4; i++) compressed.pop_front(); } for (char c : compressed) { switch(c) { case 'G': up(board, n, m); break; case 'D': down(board, n, m); break; case 'L': left(board, n, m); break; default : right(board, n, m); break; } } print_board(board, n, m); 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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | #include <iostream> #include <deque> using namespace std; enum class Edge { UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT }; enum class Direction { UP, DOWN, LEFT, RIGHT }; enum class Rotation { CLOCKWISE, COUNTER }; Direction get_dir(char c) { switch(c) { case 'G': return Direction::UP; case 'D': return Direction::DOWN; case 'L': return Direction::LEFT; default : return Direction::RIGHT; } } bool is_edge(Direction dir1, Direction dir2) { if (dir1 == Direction::UP || dir1 == Direction::DOWN) return dir2 == Direction::LEFT || dir2 == Direction::RIGHT; else return dir2 == Direction::UP || dir2 == Direction::DOWN; } Edge get_edge(Direction dir1, Direction dir2) { switch(dir1) { case Direction::UP: if (dir2 == Direction::LEFT) return Edge::UP_LEFT; else return Edge::UP_RIGHT; case Direction::DOWN: if (dir2 == Direction::LEFT) return Edge::DOWN_LEFT; else return Edge::DOWN_RIGHT; case Direction::LEFT: if (dir2 == Direction::UP) return Edge::UP_LEFT; else return Edge::DOWN_LEFT; default: if (dir2 == Direction::UP) return Edge::UP_RIGHT; else return Edge::DOWN_RIGHT; } } Rotation get_rotation(Edge edge, char c) { switch(edge) { case Edge::UP_RIGHT: if (c == 'D') return Rotation::CLOCKWISE; else return Rotation::COUNTER; case Edge::UP_LEFT: if (c == 'D') return Rotation::COUNTER; else return Rotation::CLOCKWISE; case Edge::DOWN_LEFT: if (c == 'G') return Rotation::CLOCKWISE; else return Rotation::COUNTER; default: if (c == 'G') return Rotation::COUNTER; else return Rotation::CLOCKWISE; } } Edge move_edge(char c, Edge edge) { switch(edge) { case Edge::UP_LEFT: if (c == 'P') return Edge::UP_RIGHT; else if (c == 'D') return Edge::DOWN_LEFT; else return Edge::UP_LEFT; case Edge::UP_RIGHT: if (c == 'L') return Edge::UP_LEFT; else if (c == 'D') return Edge::DOWN_RIGHT; else return Edge::UP_RIGHT; case Edge::DOWN_LEFT: if (c == 'P') return Edge::DOWN_RIGHT; else if (c == 'G') return Edge::UP_LEFT; else return Edge::DOWN_LEFT; default: if (c == 'L') return Edge::DOWN_LEFT; else if (c == 'G') return Edge::UP_RIGHT; else return Edge::DOWN_RIGHT; } } void up(char *board[], int n, int m) { for (int j = 0; j < m; j++) { int first_empty = 0; for (int i = 0; i < n; i++) if (board[i][j] != '.') board[first_empty++][j] = board[i][j]; for (int i = first_empty; i < n; i++) board[i][j] = '.'; } } void down(char *board[], int n, int m) { for (int j = 0; j < m; j++) { int first_empty = n-1; for (int i = n-1; i >= 0; i--) if (board[i][j] != '.') board[first_empty--][j] = board[i][j]; for (int i = first_empty; i >= 0; i--) board[i][j] = '.'; } } void left(char *board[], int n, int m) { for (int i = 0; i < n; i++) { int first_empty = 0; for (int j = 0; j < m; j++) if (board[i][j] != '.') board[i][first_empty++] = board[i][j]; for (int j = first_empty; j < m; j++) board[i][j] = '.'; } } void right(char *board[], int n, int m) { for (int i = 0; i < n; i++) { int first_empty = m-1; for (int j = m-1; j >= 0; j--) if (board[i][j] != '.') board[i][first_empty--] = board[i][j]; for (int j = first_empty; j >= 0; j--) board[i][j] = '.'; } } void print_board(char *board[], int n, int m) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cout << board[i][j]; } cout << "\n"; } } void print_board_int(int *board[], int n, int m) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cout << board[i][j] << "\t"; } cout << "\n"; } } void move(char *board[], int n, int m, Direction dir) { switch(dir) { case Direction::UP: up(board, n, m); break; case Direction::DOWN: down(board, n, m); break; case Direction::LEFT: left(board, n, m); break; default: right(board, n, m); break; } } void start_position(char *board[], int n, int m, Direction dir1, Direction dir2) { move(board, n, m, dir1); move(board, n, m, dir2); } void get_permutation_board(int *permutation_board[], char *board[], int n, int m) { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (board[i][j] != '.') permutation_board[i][j] = i*m+j; else permutation_board[i][j] = -1; } void up_int(int *board[], int n, int m) { for (int j = 0; j < m; j++) { int first_empty = 0; for (int i = 0; i < n; i++) if (board[i][j] != -1) board[first_empty++][j] = board[i][j]; for (int i = first_empty; i < n; i++) board[i][j] = -1; } } void down_int(int *board[], int n, int m) { for (int j = 0; j < m; j++) { int first_empty = n-1; for (int i = n-1; i >= 0; i--) if (board[i][j] != -1) board[first_empty--][j] = board[i][j]; for (int i = first_empty; i >= 0; i--) board[i][j] = -1; } } void left_int(int *board[], int n, int m) { for (int i = 0; i < n; i++) { int first_empty = 0; for (int j = 0; j < m; j++) if (board[i][j] != -1) board[i][first_empty++] = board[i][j]; for (int j = first_empty; j < m; j++) board[i][j] = -1; } } void right_int(int *board[], int n, int m) { for (int i = 0; i < n; i++) { int first_empty = m-1; for (int j = m-1; j >= 0; j--) if (board[i][j] != -1) board[i][first_empty--] = board[i][j]; for (int j = first_empty; j >= 0; j--) board[i][j] = -1; } } void rotate(int *board[], int n, int m, Edge edge, Rotation rotation) { switch (edge) { case Edge::UP_RIGHT: if (rotation == Rotation::CLOCKWISE) { down_int(board, n, m); left_int(board, n, m); up_int(board, n, m); right_int(board, n, m); } else { left_int(board, n, m); down_int(board, n, m); right_int(board, n, m); up_int(board, n, m); } break; case Edge::DOWN_RIGHT: if (rotation == Rotation::CLOCKWISE) { left_int(board, n, m); up_int(board, n, m); right_int(board, n, m); down_int(board, n, m); } else { up_int(board, n, m); left_int(board, n, m); down_int(board, n, m); right_int(board, n, m); } break; case Edge::DOWN_LEFT: if (rotation == Rotation::CLOCKWISE) { up_int(board, n, m); right_int(board, n, m); down_int(board, n, m); left_int(board, n, m); } else { right_int(board, n, m); up_int(board, n, m); left_int(board, n, m); down_int(board, n, m); } break; default: if (rotation == Rotation::CLOCKWISE) { right_int(board, n, m); down_int(board, n, m); left_int(board, n, m); up_int(board, n, m); } else { down_int(board, n, m); right_int(board, n, m); up_int(board, n, m); left_int(board, n, m); } break; } } void apply_permutation(char *board[], int *permutation[], int n, int m) { char *tmp[n]; for (int i = 0; i < n; i++) tmp[i] = new char[m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) tmp[i][j] = board[i][j]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int num = permutation[i][j]; if (num != -1) board[i][j] = tmp[num/m][num%m]; } } } void power_permutation(int *permutation[], int n, int m) { int *tmp[n]; for (int i = 0; i < n; i++) tmp[i] = new int[m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) tmp[i][j] = permutation[i][j]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int num = tmp[i][j]; if (num != -1) permutation[i][j] = tmp[num/m][num%m]; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; char *board[n]; for (int i = 0; i < n; i++) board[i] = new char[m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { cin >> board[i][j]; } int k; cin >> k; char moves[k]; for (int i = 0; i < k; i++) cin >> moves[i]; Direction dir1, dir2 = get_dir(moves[0]); int i = 1; bool end = i >= k; while (!end) { dir1 = dir2; dir2 = get_dir(moves[i++]); end = i >= k || is_edge(dir1, dir2); } if (!is_edge(dir1, dir2)) { move(board, n, m, dir2); print_board(board, n, m); return 0; } start_position(board, n, m, dir1, dir2); Edge edge, start_edge = get_edge(dir1, dir2); edge = start_edge; deque<int> compressed; for (; i < k; i++) { if (edge == move_edge(moves[i], edge)) continue; if (!compressed.empty()) { if ((moves[i] == 'L' && compressed.back() == 'P') || (moves[i] == 'G' && compressed.back() == 'D') || (moves[i] == 'P' && compressed.back() == 'L') || (moves[i] == 'D' && compressed.back() == 'G')) { edge = move_edge(moves[i], edge); compressed.pop_back(); continue; } } compressed.push_back(moves[i]); edge = move_edge(moves[i], edge); } Rotation rotation = get_rotation(start_edge, compressed.front()); int *permutation_board[n]; for (int i = 0; i < n; i++) permutation_board[i] = new int[m]; get_permutation_board(permutation_board, board, n, m); rotate(permutation_board, n, m, start_edge, rotation); int l = compressed.size()/4; while (l > 0) { if (l % 2 == 1) apply_permutation(board, permutation_board, n, m); l /= 2; power_permutation(permutation_board, n, m); } while (compressed.size() >= 4) { for (int i = 0; i < 4; i++) compressed.pop_front(); } for (char c : compressed) { switch(c) { case 'G': up(board, n, m); break; case 'D': down(board, n, m); break; case 'L': left(board, n, m); break; default : right(board, n, m); break; } } print_board(board, n, m); return 0; } |