#include <bits/stdc++.h> using namespace std; int getAxis(char c) { return c == 'L' || c == 'P'; } void simulate(int n, int m, char c, vector <vector<int>> &tab) { if (getAxis(c)) { for (int i = 0; i < n; i++) { vector <int> row; for (int j = 0; j < m; j++) if (tab[i][j] != -1) { row.push_back(tab[i][j]); tab[i][j] = -1; } int rowSize = row.size(); for (int j = 0; j < rowSize; j++) { int pos = c == 'L' ? j : m - (rowSize - j); tab[i][pos] = row[j]; } } } else { for (int j = 0; j < m; j++) { vector <int> col; for (int i = 0; i < n; i++) if (tab[i][j] != -1) { col.push_back(tab[i][j]); tab[i][j] = -1; } int colSize = col.size(); for (int i = 0; i < colSize; i++) { int pos = c == 'G' ? i : n - (colSize - i); tab[pos][j] = col[i]; } } } } vector <int> permCompose(vector <int> &p, vector <int> &q) { vector <int> res(p.size()); for (int i = 0; i < (int) p.size(); i++) { res[i] = q[p[i]]; } return res; } vector <int> permExpo(vector <int> &p, int n) { if (n == 1) { return p; } else { auto q = permExpo(p, n / 2); q = permCompose(q, q); if (n % 2) { q = permCompose(q, p); } return q; } } void solveCorner(int n, int m, int v, vector <vector<int>> &tab, string &ops) { string opsRemain; for (char c : ops) { if (opsRemain.empty()) { opsRemain.push_back(c); } else if (opsRemain.back() != c) { if (getAxis(opsRemain.back()) == getAxis(c)) { opsRemain.pop_back(); } int s = opsRemain.size(); if (s >= 2 && opsRemain[s - 2] == c) { continue; } opsRemain.push_back(c); } } int k = opsRemain.size(); int idx = 0; while ((k - idx) % 4) { simulate(n, m, opsRemain[idx++], tab); } if (idx < k) { for (int i = 0; i < 4; i++) { simulate(n, m, opsRemain[idx++], tab); } if (idx < k) { auto tabBefore = tab; for (int i = 0; i < 4; i++) { simulate(n, m, opsRemain[idx++], tab); } int numReps = (k - idx) / 4; if (numReps > 0) { vector <int> perm(v); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (tab[i][j] != -1) { perm[tabBefore[i][j]] = tab[i][j]; } } perm = permExpo(perm, numReps); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (tab[i][j] != -1) { tab[i][j] = perm[tab[i][j]]; } } } } } } void solve(int n, int m, int k, int v, vector <vector<int>> &tab, string &ops) { int idx = 1; while (idx < k && getAxis(ops[idx]) == getAxis(ops[idx - 1])) { idx++; } simulate(n, m, ops[idx - 1], tab); if (idx < k) { simulate(n, m, ops[idx], tab); } if (idx < k - 1) { ops = ops.substr(idx + 1); solveCorner(n, m, v, tab, ops); } } int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; int id = 0; vector <char> idToColor; vector <vector<int>> tab(n, vector <int> (m, -1)); for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < m; j++) if (s[j] != '.') { tab[i][j] = id++; idToColor.push_back(s[j]); } } int k; cin >> k; string ops; cin >> ops; solve(n, m, k, id, tab, ops); for (int i = 0; i < n; i++) { string s(m, '.'); for (int j = 0; j < m; j++) if (tab[i][j] != -1) { s[j] = idToColor[tab[i][j]]; } cout << s << '\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 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | #include <bits/stdc++.h> using namespace std; int getAxis(char c) { return c == 'L' || c == 'P'; } void simulate(int n, int m, char c, vector <vector<int>> &tab) { if (getAxis(c)) { for (int i = 0; i < n; i++) { vector <int> row; for (int j = 0; j < m; j++) if (tab[i][j] != -1) { row.push_back(tab[i][j]); tab[i][j] = -1; } int rowSize = row.size(); for (int j = 0; j < rowSize; j++) { int pos = c == 'L' ? j : m - (rowSize - j); tab[i][pos] = row[j]; } } } else { for (int j = 0; j < m; j++) { vector <int> col; for (int i = 0; i < n; i++) if (tab[i][j] != -1) { col.push_back(tab[i][j]); tab[i][j] = -1; } int colSize = col.size(); for (int i = 0; i < colSize; i++) { int pos = c == 'G' ? i : n - (colSize - i); tab[pos][j] = col[i]; } } } } vector <int> permCompose(vector <int> &p, vector <int> &q) { vector <int> res(p.size()); for (int i = 0; i < (int) p.size(); i++) { res[i] = q[p[i]]; } return res; } vector <int> permExpo(vector <int> &p, int n) { if (n == 1) { return p; } else { auto q = permExpo(p, n / 2); q = permCompose(q, q); if (n % 2) { q = permCompose(q, p); } return q; } } void solveCorner(int n, int m, int v, vector <vector<int>> &tab, string &ops) { string opsRemain; for (char c : ops) { if (opsRemain.empty()) { opsRemain.push_back(c); } else if (opsRemain.back() != c) { if (getAxis(opsRemain.back()) == getAxis(c)) { opsRemain.pop_back(); } int s = opsRemain.size(); if (s >= 2 && opsRemain[s - 2] == c) { continue; } opsRemain.push_back(c); } } int k = opsRemain.size(); int idx = 0; while ((k - idx) % 4) { simulate(n, m, opsRemain[idx++], tab); } if (idx < k) { for (int i = 0; i < 4; i++) { simulate(n, m, opsRemain[idx++], tab); } if (idx < k) { auto tabBefore = tab; for (int i = 0; i < 4; i++) { simulate(n, m, opsRemain[idx++], tab); } int numReps = (k - idx) / 4; if (numReps > 0) { vector <int> perm(v); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (tab[i][j] != -1) { perm[tabBefore[i][j]] = tab[i][j]; } } perm = permExpo(perm, numReps); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (tab[i][j] != -1) { tab[i][j] = perm[tab[i][j]]; } } } } } } void solve(int n, int m, int k, int v, vector <vector<int>> &tab, string &ops) { int idx = 1; while (idx < k && getAxis(ops[idx]) == getAxis(ops[idx - 1])) { idx++; } simulate(n, m, ops[idx - 1], tab); if (idx < k) { simulate(n, m, ops[idx], tab); } if (idx < k - 1) { ops = ops.substr(idx + 1); solveCorner(n, m, v, tab, ops); } } int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; int id = 0; vector <char> idToColor; vector <vector<int>> tab(n, vector <int> (m, -1)); for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < m; j++) if (s[j] != '.') { tab[i][j] = id++; idToColor.push_back(s[j]); } } int k; cin >> k; string ops; cin >> ops; solve(n, m, k, id, tab, ops); for (int i = 0; i < n; i++) { string s(m, '.'); for (int j = 0; j < m; j++) if (tab[i][j] != -1) { s[j] = idToColor[tab[i][j]]; } cout << s << '\n'; } return 0; } |