#include <cstdio> #include <vector> using namespace std; struct Board { int n, m; char board[500][500]; void rotate(char direction) { if (direction == 'G') { for (int i=0; i<this->m; i++) { vector<char> blocks; for (int j=0; j<this->n; j++) { if (this->board[j][i] != '.') { blocks.push_back(board[j][i]); } this->board[j][i] = '.'; } for (int j=0; j<blocks.size(); j++) { this->board[j][i] = blocks[j]; } } } else if (direction == 'D') { for (int i=0; i<this->m; i++) { vector<char> blocks; for (int j=this->n - 1; j>=0; j--) { if (this->board[j][i] != '.') { blocks.push_back(board[j][i]); } this->board[j][i] = '.'; } for (int j=0; j<blocks.size(); j++) { this->board[this->n - j - 1][i] = blocks[j]; } } } else if (direction == 'L') { for (int i=0; i<this->n; i++) { vector<char> blocks; for (int j=0; j<this->m; j++) { if (this->board[i][j] != '.') { blocks.push_back(board[i][j]); } this->board[i][j] = '.'; } for (int j=0; j<blocks.size(); j++) { this->board[i][j] = blocks[j]; } } } else if (direction == 'P') { for (int i=0; i<this->n; i++) { vector<char> blocks; for (int j=this->m - 1; j>=0; j--) { if (this->board[i][j] != '.') { blocks.push_back(board[i][j]); } this->board[i][j] = '.'; } for (int j=0; j<blocks.size(); j++) { this->board[i][this->m - j - 1] = blocks[j]; } } } } void read() { int n, m; scanf("%d %d", &n, &m); this->n = n; this->m = m; for (int i=0; i<n; i++) { scanf("%s", &this->board[i]); } } void print() { for (int i=0; i<this->n; i++) { for (int j=0; j<this->m; j++) { printf("%c", this->board[i][j]); } puts(""); } } }; int main() { int k; char moves[500001]; Board initialBoard; initialBoard.read(); scanf("%d", &k); scanf("%s", &moves); for (int i=0; i<k; i++) { initialBoard.rotate(moves[i]); } initialBoard.print(); }
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 | #include <cstdio> #include <vector> using namespace std; struct Board { int n, m; char board[500][500]; void rotate(char direction) { if (direction == 'G') { for (int i=0; i<this->m; i++) { vector<char> blocks; for (int j=0; j<this->n; j++) { if (this->board[j][i] != '.') { blocks.push_back(board[j][i]); } this->board[j][i] = '.'; } for (int j=0; j<blocks.size(); j++) { this->board[j][i] = blocks[j]; } } } else if (direction == 'D') { for (int i=0; i<this->m; i++) { vector<char> blocks; for (int j=this->n - 1; j>=0; j--) { if (this->board[j][i] != '.') { blocks.push_back(board[j][i]); } this->board[j][i] = '.'; } for (int j=0; j<blocks.size(); j++) { this->board[this->n - j - 1][i] = blocks[j]; } } } else if (direction == 'L') { for (int i=0; i<this->n; i++) { vector<char> blocks; for (int j=0; j<this->m; j++) { if (this->board[i][j] != '.') { blocks.push_back(board[i][j]); } this->board[i][j] = '.'; } for (int j=0; j<blocks.size(); j++) { this->board[i][j] = blocks[j]; } } } else if (direction == 'P') { for (int i=0; i<this->n; i++) { vector<char> blocks; for (int j=this->m - 1; j>=0; j--) { if (this->board[i][j] != '.') { blocks.push_back(board[i][j]); } this->board[i][j] = '.'; } for (int j=0; j<blocks.size(); j++) { this->board[i][this->m - j - 1] = blocks[j]; } } } } void read() { int n, m; scanf("%d %d", &n, &m); this->n = n; this->m = m; for (int i=0; i<n; i++) { scanf("%s", &this->board[i]); } } void print() { for (int i=0; i<this->n; i++) { for (int j=0; j<this->m; j++) { printf("%c", this->board[i][j]); } puts(""); } } }; int main() { int k; char moves[500001]; Board initialBoard; initialBoard.read(); scanf("%d", &k); scanf("%s", &moves); for (int i=0; i<k; i++) { initialBoard.rotate(moves[i]); } initialBoard.print(); } |