#include <iostream> #include <map> using namespace std; int n, m, k; char tab[507][507]; string moves; bool hor = false, ver = false; void slideG() { ver = true; for (int j = 0; j < m; j++) { for (int i = 1; i < n; i++) { int k = i; while (k > 0 && tab[k - 1][j] == '.') { swap(tab[k][j], tab[k - 1][j]); k--; } } } } void slideD() { ver = true; for (int j = 0; j < m; j++) { for (int i = n - 2; i >= 0; i--) { int k = i; while (k < n - 1 && tab[k + 1][j] == '.') { swap(tab[k][j], tab[k + 1][j]); k++; } } } } void slideL() { hor = true; for (int i = 0; i < n; i++) { for (int j = 1; j < m; j++) { int k = j; while (k > 0 && tab[i][k - 1] == '.') { swap(tab[i][k], tab[i][k - 1]); k--; } } } } void slideP() { hor = true; for (int i = 0; i < n; i++) { for (int j = m - 2; j >= 0; j--) { int k = j; while (k < m - 1 && tab[i][k + 1] == '.') { swap(tab[i][k], tab[i][k + 1]); k++; } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> tab[i][j]; cin >> k; cin >> moves; map<char, int> mp; int z = 0; for (auto i : moves) { if (i == 'G' && !ver) { slideG(); } else if (i == 'D' && !ver) { slideD(); } else if (i == 'L' && !hor) { slideL(); } else if (i == 'P' && !hor) { slideP(); } else { mp[i] = ++z; } ver = false; hor = false; } if (mp['L'] > mp['P']) slideL(); else if (mp['P'] != 0) slideP(); if (mp['G'] > mp['D']) slideG(); else if (mp['D'] != 0) slideD(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cout << tab[i][j]; cout << '\n'; } }
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> #include <map> using namespace std; int n, m, k; char tab[507][507]; string moves; bool hor = false, ver = false; void slideG() { ver = true; for (int j = 0; j < m; j++) { for (int i = 1; i < n; i++) { int k = i; while (k > 0 && tab[k - 1][j] == '.') { swap(tab[k][j], tab[k - 1][j]); k--; } } } } void slideD() { ver = true; for (int j = 0; j < m; j++) { for (int i = n - 2; i >= 0; i--) { int k = i; while (k < n - 1 && tab[k + 1][j] == '.') { swap(tab[k][j], tab[k + 1][j]); k++; } } } } void slideL() { hor = true; for (int i = 0; i < n; i++) { for (int j = 1; j < m; j++) { int k = j; while (k > 0 && tab[i][k - 1] == '.') { swap(tab[i][k], tab[i][k - 1]); k--; } } } } void slideP() { hor = true; for (int i = 0; i < n; i++) { for (int j = m - 2; j >= 0; j--) { int k = j; while (k < m - 1 && tab[i][k + 1] == '.') { swap(tab[i][k], tab[i][k + 1]); k++; } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> tab[i][j]; cin >> k; cin >> moves; map<char, int> mp; int z = 0; for (auto i : moves) { if (i == 'G' && !ver) { slideG(); } else if (i == 'D' && !ver) { slideD(); } else if (i == 'L' && !hor) { slideL(); } else if (i == 'P' && !hor) { slideP(); } else { mp[i] = ++z; } ver = false; hor = false; } if (mp['L'] > mp['P']) slideL(); else if (mp['P'] != 0) slideP(); if (mp['G'] > mp['D']) slideG(); else if (mp['D'] != 0) slideD(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cout << tab[i][j]; cout << '\n'; } } |