//2022-12-16 //author: Marcin Rolbiecki #include <bits/stdc++.h> using namespace std; const int maxN = 510; int n, m, nruchy; char G[maxN][maxN]; string ruchy; int main () { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> G[i][j]; cin >> nruchy; bool p, l, g, d; p = l = g = d = 1; for (int i = 0; i < nruchy; i++) { char ch; cin >> ch; if (ch == 'P' && p) { ruchy.push_back(ch); p = 0; l = 1; } if (ch == 'L' && l) { ruchy.push_back(ch); l = 0; p = 1; } if (ch == 'G' && g) { ruchy.push_back(ch); g = 0; d = 1; } if (ch == 'D' && d) { ruchy.push_back(ch); d = 0; g = 1; } } nruchy = ruchy.size(); for (int r = 0; r < nruchy; r++) { if (ruchy[r] == 'P') { for (int i = 0; i < n; i++) { int wolne = m-1; for (int j = m-1; j >= 0; j--) { while (wolne > 0 && G[i][wolne] != '.') wolne--; if (j < wolne && G[i][j] != '.') swap(G[i][j], G[i][wolne]); } } } if (ruchy[r] == 'L') { for (int i = 0; i < n; i++) { int wolne = 0; for (int j = 0; j < m; j++) { while (wolne < m-1 && G[i][wolne] != '.') wolne++; if (j > wolne && G[i][j] != '.') swap(G[i][j], G[i][wolne]); } } } if (ruchy[r] == 'G') { for (int j = 0; j < m; j++) { int wolne = 0; for (int i = 0; i < n; i++) { while (wolne < n-1 && G[wolne][j] != '.') wolne++; if (i > wolne && G[i][j] != '.') swap(G[i][j], G[wolne][j]); } } } if (ruchy[r] == 'D') { for (int j = 0; j < m; j++) { int wolne = n-1; for (int i = n-1; i >= 0; i--) { while (wolne > 0 && G[wolne][j] != '.') wolne--; if (i < wolne && G[i][j] != '.') swap(G[i][j], G[wolne][j]); } } } /* for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cout << G[i][j]; cout << '\n'; } cout << endl; */ } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cout << G[i][j]; cout << '\n'; } return 0; } /* 3 3 CCB BC. ... 16 PDGLPGLDPDGLPDGL 3 3 CCB BC. ... 4 PGLD */
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 | //2022-12-16 //author: Marcin Rolbiecki #include <bits/stdc++.h> using namespace std; const int maxN = 510; int n, m, nruchy; char G[maxN][maxN]; string ruchy; int main () { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> G[i][j]; cin >> nruchy; bool p, l, g, d; p = l = g = d = 1; for (int i = 0; i < nruchy; i++) { char ch; cin >> ch; if (ch == 'P' && p) { ruchy.push_back(ch); p = 0; l = 1; } if (ch == 'L' && l) { ruchy.push_back(ch); l = 0; p = 1; } if (ch == 'G' && g) { ruchy.push_back(ch); g = 0; d = 1; } if (ch == 'D' && d) { ruchy.push_back(ch); d = 0; g = 1; } } nruchy = ruchy.size(); for (int r = 0; r < nruchy; r++) { if (ruchy[r] == 'P') { for (int i = 0; i < n; i++) { int wolne = m-1; for (int j = m-1; j >= 0; j--) { while (wolne > 0 && G[i][wolne] != '.') wolne--; if (j < wolne && G[i][j] != '.') swap(G[i][j], G[i][wolne]); } } } if (ruchy[r] == 'L') { for (int i = 0; i < n; i++) { int wolne = 0; for (int j = 0; j < m; j++) { while (wolne < m-1 && G[i][wolne] != '.') wolne++; if (j > wolne && G[i][j] != '.') swap(G[i][j], G[i][wolne]); } } } if (ruchy[r] == 'G') { for (int j = 0; j < m; j++) { int wolne = 0; for (int i = 0; i < n; i++) { while (wolne < n-1 && G[wolne][j] != '.') wolne++; if (i > wolne && G[i][j] != '.') swap(G[i][j], G[wolne][j]); } } } if (ruchy[r] == 'D') { for (int j = 0; j < m; j++) { int wolne = n-1; for (int i = n-1; i >= 0; i--) { while (wolne > 0 && G[wolne][j] != '.') wolne--; if (i < wolne && G[i][j] != '.') swap(G[i][j], G[wolne][j]); } } } /* for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cout << G[i][j]; cout << '\n'; } cout << endl; */ } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cout << G[i][j]; cout << '\n'; } return 0; } /* 3 3 CCB BC. ... 16 PDGLPGLDPDGLPDGL 3 3 CCB BC. ... 4 PGLD */ |