#include <iostream> using namespace std; void move(int y, int x, int**& blocks, char move) { int** newblocks = new int* [x]; for (int i = 0; i < x; i++) newblocks[i] = new int[y]{0}; int pos = 0; if (move == 'G') { pos = 0; for (int i = 0; i < x; i++) { pos = 0; for (int j = 0; j < y; j++) { if (blocks[i][j] != '.') { newblocks[i][pos] = blocks[i][j]; pos++; } } } blocks = newblocks; return; } if (move == 'D') { pos = y-1; for (int i = 0; i < x; i++) { pos = y-1; for (int j = y-1; j >= 0; j--) { if (blocks[i][j] != '.') { newblocks[i][pos] = blocks[i][j]; pos--; } } } blocks = newblocks; return; } if (move == 'L') { pos = 0; for (int i = 0; i < y; i++) { pos = 0; for (int j = 0; j < x; j++) { if (blocks[j][i] != '.') { newblocks[pos][i] = blocks[j][i]; pos++; } } } blocks = newblocks; return; } if (move == 'P') { pos = x-1; for (int i = 0; i < y; i++) { pos = x-1; for (int j = x-1; j >=0; j--) { if (blocks[j][i] =='B'|| blocks[j][i]=='C') { newblocks[pos][i] = blocks[j][i]; pos--; } } } blocks = newblocks; return; } } int main() { int n, m; cin >> n >> m; int** blocks = new int* [m]; for (int i = 0; i < m; i++) { blocks[i] = new int[n]; } char a; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a; blocks[j][i] = a; } } int k; cin >> k; char mv; for (int i = 0; i < k; i++) { cin >> mv; move(n, m, blocks, mv); for (int l = 0; l < n; l++) { for (int j = 0; j < m; j++) { if (blocks[j][l] == 0) blocks[j][l] = '.'; } } } for (int l = 0; l < n; l++) { for (int j = 0; j < m; j++) { if (blocks[j][l] == 0) blocks[j][l] = '.'; cout << (char)blocks[j][l]; } cout << endl; } 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 | #include <iostream> using namespace std; void move(int y, int x, int**& blocks, char move) { int** newblocks = new int* [x]; for (int i = 0; i < x; i++) newblocks[i] = new int[y]{0}; int pos = 0; if (move == 'G') { pos = 0; for (int i = 0; i < x; i++) { pos = 0; for (int j = 0; j < y; j++) { if (blocks[i][j] != '.') { newblocks[i][pos] = blocks[i][j]; pos++; } } } blocks = newblocks; return; } if (move == 'D') { pos = y-1; for (int i = 0; i < x; i++) { pos = y-1; for (int j = y-1; j >= 0; j--) { if (blocks[i][j] != '.') { newblocks[i][pos] = blocks[i][j]; pos--; } } } blocks = newblocks; return; } if (move == 'L') { pos = 0; for (int i = 0; i < y; i++) { pos = 0; for (int j = 0; j < x; j++) { if (blocks[j][i] != '.') { newblocks[pos][i] = blocks[j][i]; pos++; } } } blocks = newblocks; return; } if (move == 'P') { pos = x-1; for (int i = 0; i < y; i++) { pos = x-1; for (int j = x-1; j >=0; j--) { if (blocks[j][i] =='B'|| blocks[j][i]=='C') { newblocks[pos][i] = blocks[j][i]; pos--; } } } blocks = newblocks; return; } } int main() { int n, m; cin >> n >> m; int** blocks = new int* [m]; for (int i = 0; i < m; i++) { blocks[i] = new int[n]; } char a; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a; blocks[j][i] = a; } } int k; cin >> k; char mv; for (int i = 0; i < k; i++) { cin >> mv; move(n, m, blocks, mv); for (int l = 0; l < n; l++) { for (int j = 0; j < m; j++) { if (blocks[j][l] == 0) blocks[j][l] = '.'; } } } for (int l = 0; l < n; l++) { for (int j = 0; j < m; j++) { if (blocks[j][l] == 0) blocks[j][l] = '.'; cout << (char)blocks[j][l]; } cout << endl; } return 0; } |