#include <iostream> #define SIZE 507 using namespace std; char a[SIZE][SIZE]; int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; for (int y = 0; y < n; y++) for (int x = 0; x < m; x++) cin >> a[x][y]; int k; cin >> k; for (int i = 0; i < k; i++) { char move; cin >> move; if (move == 'G') { for (int x = 0; x < m; x++) for (int y = 1; y < n; y++) if (a[x][y] != '.') { int i = y - 1; while (i >= 0 && a[x][i] == '.') i--; if (a[x][i + 1] == '.') { a[x][i + 1] = a[x][y]; a[x][y] = '.'; } } } if (move == 'D') { for (int x = 0; x < m; x++) for (int y = n - 2; y >= 0; y--) if (a[x][y] != '.') { int i = y + 1; while (i <= (n - 1) && a[x][i] == '.') i++; if (a[x][i - 1] == '.') { a[x][i - 1] = a[x][y]; a[x][y] = '.'; } } } if (move == 'L') { for (int y = 0; y < n; y++) for (int x = 1; x < m; x++) if (a[x][y] != '.') { int i = x - 1; while (i >= 0 && a[i][y] == '.') i--; if (a[i + 1][y] == '.') { a[i + 1][y] = a[x][y]; a[x][y] = '.'; } } } if (move == 'P') { for (int y = 0; y < n; y++) for (int x = m - 2; x >= 0; x--) if (a[x][y] != '.') { int i = x + 1; while (i <= (m - 1) && a[i][y] == '.') i++; if (a[i - 1][y] == '.') { a[i - 1][y] = a[x][y]; a[x][y] = '.'; } } } } for (int y = 0; y < n; y++) { for (int x = 0; x < m; x++) cout << a[x][y]; cout << '\n'; } cout << '\n' << '\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 | #include <iostream> #define SIZE 507 using namespace std; char a[SIZE][SIZE]; int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; for (int y = 0; y < n; y++) for (int x = 0; x < m; x++) cin >> a[x][y]; int k; cin >> k; for (int i = 0; i < k; i++) { char move; cin >> move; if (move == 'G') { for (int x = 0; x < m; x++) for (int y = 1; y < n; y++) if (a[x][y] != '.') { int i = y - 1; while (i >= 0 && a[x][i] == '.') i--; if (a[x][i + 1] == '.') { a[x][i + 1] = a[x][y]; a[x][y] = '.'; } } } if (move == 'D') { for (int x = 0; x < m; x++) for (int y = n - 2; y >= 0; y--) if (a[x][y] != '.') { int i = y + 1; while (i <= (n - 1) && a[x][i] == '.') i++; if (a[x][i - 1] == '.') { a[x][i - 1] = a[x][y]; a[x][y] = '.'; } } } if (move == 'L') { for (int y = 0; y < n; y++) for (int x = 1; x < m; x++) if (a[x][y] != '.') { int i = x - 1; while (i >= 0 && a[i][y] == '.') i--; if (a[i + 1][y] == '.') { a[i + 1][y] = a[x][y]; a[x][y] = '.'; } } } if (move == 'P') { for (int y = 0; y < n; y++) for (int x = m - 2; x >= 0; x--) if (a[x][y] != '.') { int i = x + 1; while (i <= (m - 1) && a[i][y] == '.') i++; if (a[i - 1][y] == '.') { a[i - 1][y] = a[x][y]; a[x][y] = '.'; } } } } for (int y = 0; y < n; y++) { for (int x = 0; x < m; x++) cout << a[x][y]; cout << '\n'; } cout << '\n' << '\n'; return 0; } |