#include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) int n, m; template<class T, class Vec> void shake(vector<Vec>& mat, char dir, T dot) { if (dir == 'L') { rep(i, 0, n) { int pos = 0; rep(j, 0, m) { if (mat[i][j] != dot) { mat[i][pos++] = mat[i][j]; } } while (pos < m) mat[i][pos++] = dot; } } else if (dir == 'P') { rep(i, 0, n) { int pos = m-1; for (int j = m-1; j >= 0; j--) { if (mat[i][j] != dot) { mat[i][pos--] = mat[i][j]; } } while (pos >= 0) mat[i][pos--] = dot; } } else if (dir == 'G') { rep(i, 0, m) { int pos = 0; rep(j, 0, n) { if (mat[j][i] != dot) { mat[pos++][i] = mat[j][i]; } } while (pos < n) mat[pos++][i] = dot; } } else if (dir == 'D') { rep(i, 0, m) { int pos = n-1; for (int j = n-1; j >= 0; j--) { if (mat[j][i] != dot) { mat[pos--][i] = mat[j][i]; } } while (pos >= 0) mat[pos--][i] = dot; } } else { assert(0); } } void merge(vector<vector<pii>> p, vector<vector<pii>>& q) { rep(i, 0, n) rep(j, 0, m) { if (q[i][j].x != -1) { q[i][j] = p[q[i][j].x][q[i][j].y]; } } } vector<vector<pii>> pow(vector<vector<pii>> p, int t) { vector<vector<pii>> ret; while (t > 0) { if (t % 2) { if (ret.empty()) ret = p; else merge(p, ret); } merge(p, p); t /= 2; } return ret; } vector<string> run() { cin >> n >> m; vector<string> mat(n); each(s, mat) cin >> s; int k; string moves; cin >> k >> moves; int startCorner = -1, corner = -1, rot = 0; char last = 0; each(c, moves) if (c != last) { if (startCorner == -1) { if (last == 'L') { if (c == 'D') startCorner = 0; else if (c == 'G') startCorner = 3; } else if (last == 'P') { if (c == 'D') startCorner = 1; else if (c == 'G') startCorner = 2; } else if (last == 'D') { if (c == 'L') startCorner = 0; else if (c == 'P') startCorner = 1; } else if (last == 'G') { if (c == 'L') startCorner = 3; else if (c == 'P') startCorner = 2; } if (startCorner != -1) { shake(mat, last, '.'); shake(mat, c, '.'); corner = startCorner; } } else { if (corner == 0) { if (c == 'G') corner = 3, rot--; else if (c == 'P') corner = 1, rot++; } else if (corner == 1) { if (c == 'G') corner = 2, rot++; else if (c == 'L') corner = 0, rot--; } else if (corner == 2) { if (c == 'D') corner = 1, rot--; else if (c == 'L') corner = 3, rot++; } else if (corner == 3) { if (c == 'D') corner = 0, rot++; else if (c == 'P') corner = 2, rot--; } else { assert(0); } } last = c; } if (startCorner == -1) { shake(mat, last, '.'); return mat; } if (rot == 0) { return mat; } string ord; if (rot > 0) { ord = "PGLD"; rotate(ord.begin(), ord.begin()+startCorner, ord.end()); } else if (rot < 0) { ord = "GPDL"; if (startCorner != 0) { rotate(ord.begin(), ord.begin()+4-startCorner, ord.end()); } rot *= -1; } if (rot >= 4) { vector<vector<pii>> inds(n, vector<pii>(m, mp(-1, -1))); rep(i, 0, n) rep(j, 0, m) { if (mat[i][j] != '.') { inds[i][j] = {i,j}; } } rep(i, 0, 4) { shake(inds, ord[i%4], mp(-1, -1)); } inds = pow(inds, rot/4); rot %= 4; vector<string> mat2(n, string(m, '.')); rep(i, 0, n) rep(j, 0, m) { if (inds[i][j] != mp(-1, -1)) { mat2[i][j] = mat[inds[i][j].x][inds[i][j].y]; } } mat = move(mat2); } rep(i, 0, rot) { shake(mat, ord[i%4], '.'); } return mat; } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); auto ans = run(); each(row, ans) cout << row << '\n'; cout << flush; _Exit(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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | #include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) int n, m; template<class T, class Vec> void shake(vector<Vec>& mat, char dir, T dot) { if (dir == 'L') { rep(i, 0, n) { int pos = 0; rep(j, 0, m) { if (mat[i][j] != dot) { mat[i][pos++] = mat[i][j]; } } while (pos < m) mat[i][pos++] = dot; } } else if (dir == 'P') { rep(i, 0, n) { int pos = m-1; for (int j = m-1; j >= 0; j--) { if (mat[i][j] != dot) { mat[i][pos--] = mat[i][j]; } } while (pos >= 0) mat[i][pos--] = dot; } } else if (dir == 'G') { rep(i, 0, m) { int pos = 0; rep(j, 0, n) { if (mat[j][i] != dot) { mat[pos++][i] = mat[j][i]; } } while (pos < n) mat[pos++][i] = dot; } } else if (dir == 'D') { rep(i, 0, m) { int pos = n-1; for (int j = n-1; j >= 0; j--) { if (mat[j][i] != dot) { mat[pos--][i] = mat[j][i]; } } while (pos >= 0) mat[pos--][i] = dot; } } else { assert(0); } } void merge(vector<vector<pii>> p, vector<vector<pii>>& q) { rep(i, 0, n) rep(j, 0, m) { if (q[i][j].x != -1) { q[i][j] = p[q[i][j].x][q[i][j].y]; } } } vector<vector<pii>> pow(vector<vector<pii>> p, int t) { vector<vector<pii>> ret; while (t > 0) { if (t % 2) { if (ret.empty()) ret = p; else merge(p, ret); } merge(p, p); t /= 2; } return ret; } vector<string> run() { cin >> n >> m; vector<string> mat(n); each(s, mat) cin >> s; int k; string moves; cin >> k >> moves; int startCorner = -1, corner = -1, rot = 0; char last = 0; each(c, moves) if (c != last) { if (startCorner == -1) { if (last == 'L') { if (c == 'D') startCorner = 0; else if (c == 'G') startCorner = 3; } else if (last == 'P') { if (c == 'D') startCorner = 1; else if (c == 'G') startCorner = 2; } else if (last == 'D') { if (c == 'L') startCorner = 0; else if (c == 'P') startCorner = 1; } else if (last == 'G') { if (c == 'L') startCorner = 3; else if (c == 'P') startCorner = 2; } if (startCorner != -1) { shake(mat, last, '.'); shake(mat, c, '.'); corner = startCorner; } } else { if (corner == 0) { if (c == 'G') corner = 3, rot--; else if (c == 'P') corner = 1, rot++; } else if (corner == 1) { if (c == 'G') corner = 2, rot++; else if (c == 'L') corner = 0, rot--; } else if (corner == 2) { if (c == 'D') corner = 1, rot--; else if (c == 'L') corner = 3, rot++; } else if (corner == 3) { if (c == 'D') corner = 0, rot++; else if (c == 'P') corner = 2, rot--; } else { assert(0); } } last = c; } if (startCorner == -1) { shake(mat, last, '.'); return mat; } if (rot == 0) { return mat; } string ord; if (rot > 0) { ord = "PGLD"; rotate(ord.begin(), ord.begin()+startCorner, ord.end()); } else if (rot < 0) { ord = "GPDL"; if (startCorner != 0) { rotate(ord.begin(), ord.begin()+4-startCorner, ord.end()); } rot *= -1; } if (rot >= 4) { vector<vector<pii>> inds(n, vector<pii>(m, mp(-1, -1))); rep(i, 0, n) rep(j, 0, m) { if (mat[i][j] != '.') { inds[i][j] = {i,j}; } } rep(i, 0, 4) { shake(inds, ord[i%4], mp(-1, -1)); } inds = pow(inds, rot/4); rot %= 4; vector<string> mat2(n, string(m, '.')); rep(i, 0, n) rep(j, 0, m) { if (inds[i][j] != mp(-1, -1)) { mat2[i][j] = mat[inds[i][j].x][inds[i][j].y]; } } mat = move(mat2); } rep(i, 0, rot) { shake(mat, ord[i%4], '.'); } return mat; } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); auto ans = run(); each(row, ans) cout << row << '\n'; cout << flush; _Exit(0); } |