#pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (auto i=(a); i<(b); i++) #define FORD(i, a, b) for (int i=(a); i>(b); i--) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define PPC(x) __builtin_popcountll(x) #define MSB(x) (63 - __builtin_clzll(x)) #define LSB(x) __builtin_ctzll(x) #define ARG(x, i) (get<i>(x)) #define ithBit(m, i) ((m) >> (i) & 1) #define pb push_back #define ft first #define sd second #define kw(a) ((a) * (a)) #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = min(a, (T1)b); } template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = max(a, (T1)b); } const int maxN = 1 << 10, maxM = 1 << 19; int n, m; char T[maxN][maxN]; char Q[maxM]; int vis[maxN][maxN]; int G[maxN][maxN][2]; void moveHor(bool right) { FOR(i, 0, n) { string content {""}; FOR(j, 0, m) if (T[i][j] != '.') { content.pb(T[i][j]); T[i][j] = '.'; } std::copy(ALL(content), &T[i][right ? m - SZ(content) : 0]); } } void moveVer(bool down) { FOR(j, 0, m) { string content {""}; FOR(i, 0, n) if (T[i][j] != '.') { content.pb(T[i][j]); T[i][j] = '.'; } int i = down ? n - SZ(content) : 0; for (char c : content) T[i++][j] = c; } } void moveAny(char d) { if (d == 'L' or d == 'P') moveHor(d == 'P'); if (d == 'G' or d == 'D') moveVer(d == 'D'); } map <char, char> same { {'P', 'L'}, {'L', 'P'}, {'G', 'D'}, {'D', 'G'} }; char getDir() { static int i = 0; if (!Q[i]) return 0; char first = Q[i]; while (Q[i+1] == first or Q[i+1] == same[first]) i++; return Q[i++]; } void genGraph() { FOR(j, 0, m) { int i0 = 0, i1 = n-1; if (T[i0][j] == '.' and T[i1][j] == '.') continue; while (T[i0][j] == '.') i0++; while (T[i1][j] == '.') i1--; FOR(i, i0, i1+1) G[i][j][0] = i1 - (i - i0); } FOR(i, 0, n) { int j0 = 0, j1 = m-1; if (T[i][j0] == '.' and T[i][j1] == '.') continue; while (T[i][j0] == '.') j0++; while (T[i][j1] == '.') j1--; FOR(j, j0, j1+1) G[i][j][1] = j1 - (j - j0); } } void crossCycle(int i, int j, bool startHor, int r, int deg) { vector <char> C(0); vector <pair <int, int> > coors(0); int x[2] = {i, j}; int& a = x[0]; int& b = x[1]; for (int orient = startHor; vis[a][b] < deg; vis[a][b]++, x[orient] = G[a][b][orient], orient ^= 1) C.pb(T[a][b]), coors.pb({a, b}); int s = SZ(C), sgn = 1; r %= s; FOR(ind, 0, s) { int target = (ind + r * sgn + s) % s; tie(i, j) = coors[target]; T[i][j] = C[ind]; sgn = -sgn; } } void procCycles(int r, bool startHor) { genGraph(); FOR(i, 0, n) FOR(j, 0, m) if (T[i][j] != '.' and vis[i][j] == 0 and (G[i][j][0] == i or G[i][j][1] == j)) crossCycle(i, j, startHor, r, 2); FOR(i, 0, n) FOR(j, 0, m) if (T[i][j] != '.' and vis[i][j] == 0) crossCycle(i, j, startHor, r, 1); } void solve() { scanf ("%d%d", &n, &m); FOR(i, 0, n) scanf ("%s", T[i]); int q; scanf ("%d%s", &q, Q); vector <char> S; FOR(_, 0, 2) S.pb(getDir()), moveAny(S.back()); while (char d = getDir()) { if (d == same[S.back()]) { if (SZ(S) == 2) swap(S[0], S[1]), S.pb(d); else S.pop_back(); } else if (d == same[S[SZ(S)-2]]) S.pb(d); } FOR(_, 0, 2) S.erase(S.begin()); if (!S.empty()) { int r = SZ(S) % 4; procCycles(SZ(S) - r, S.front() == 'L' or S.front() == 'P'); FOR(i, SZ(S) - r, SZ(S)) moveAny(S[i]); } FOR(i, 0, n) printf("%s\n", T[i]); } int main() { int t = 1; //scanf ("%d", &t); FOR(tid, 1, t+1) { //printf("Case #%d: ", tid); solve(); } 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | #pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (auto i=(a); i<(b); i++) #define FORD(i, a, b) for (int i=(a); i>(b); i--) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define PPC(x) __builtin_popcountll(x) #define MSB(x) (63 - __builtin_clzll(x)) #define LSB(x) __builtin_ctzll(x) #define ARG(x, i) (get<i>(x)) #define ithBit(m, i) ((m) >> (i) & 1) #define pb push_back #define ft first #define sd second #define kw(a) ((a) * (a)) #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = min(a, (T1)b); } template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = max(a, (T1)b); } const int maxN = 1 << 10, maxM = 1 << 19; int n, m; char T[maxN][maxN]; char Q[maxM]; int vis[maxN][maxN]; int G[maxN][maxN][2]; void moveHor(bool right) { FOR(i, 0, n) { string content {""}; FOR(j, 0, m) if (T[i][j] != '.') { content.pb(T[i][j]); T[i][j] = '.'; } std::copy(ALL(content), &T[i][right ? m - SZ(content) : 0]); } } void moveVer(bool down) { FOR(j, 0, m) { string content {""}; FOR(i, 0, n) if (T[i][j] != '.') { content.pb(T[i][j]); T[i][j] = '.'; } int i = down ? n - SZ(content) : 0; for (char c : content) T[i++][j] = c; } } void moveAny(char d) { if (d == 'L' or d == 'P') moveHor(d == 'P'); if (d == 'G' or d == 'D') moveVer(d == 'D'); } map <char, char> same { {'P', 'L'}, {'L', 'P'}, {'G', 'D'}, {'D', 'G'} }; char getDir() { static int i = 0; if (!Q[i]) return 0; char first = Q[i]; while (Q[i+1] == first or Q[i+1] == same[first]) i++; return Q[i++]; } void genGraph() { FOR(j, 0, m) { int i0 = 0, i1 = n-1; if (T[i0][j] == '.' and T[i1][j] == '.') continue; while (T[i0][j] == '.') i0++; while (T[i1][j] == '.') i1--; FOR(i, i0, i1+1) G[i][j][0] = i1 - (i - i0); } FOR(i, 0, n) { int j0 = 0, j1 = m-1; if (T[i][j0] == '.' and T[i][j1] == '.') continue; while (T[i][j0] == '.') j0++; while (T[i][j1] == '.') j1--; FOR(j, j0, j1+1) G[i][j][1] = j1 - (j - j0); } } void crossCycle(int i, int j, bool startHor, int r, int deg) { vector <char> C(0); vector <pair <int, int> > coors(0); int x[2] = {i, j}; int& a = x[0]; int& b = x[1]; for (int orient = startHor; vis[a][b] < deg; vis[a][b]++, x[orient] = G[a][b][orient], orient ^= 1) C.pb(T[a][b]), coors.pb({a, b}); int s = SZ(C), sgn = 1; r %= s; FOR(ind, 0, s) { int target = (ind + r * sgn + s) % s; tie(i, j) = coors[target]; T[i][j] = C[ind]; sgn = -sgn; } } void procCycles(int r, bool startHor) { genGraph(); FOR(i, 0, n) FOR(j, 0, m) if (T[i][j] != '.' and vis[i][j] == 0 and (G[i][j][0] == i or G[i][j][1] == j)) crossCycle(i, j, startHor, r, 2); FOR(i, 0, n) FOR(j, 0, m) if (T[i][j] != '.' and vis[i][j] == 0) crossCycle(i, j, startHor, r, 1); } void solve() { scanf ("%d%d", &n, &m); FOR(i, 0, n) scanf ("%s", T[i]); int q; scanf ("%d%s", &q, Q); vector <char> S; FOR(_, 0, 2) S.pb(getDir()), moveAny(S.back()); while (char d = getDir()) { if (d == same[S.back()]) { if (SZ(S) == 2) swap(S[0], S[1]), S.pb(d); else S.pop_back(); } else if (d == same[S[SZ(S)-2]]) S.pb(d); } FOR(_, 0, 2) S.erase(S.begin()); if (!S.empty()) { int r = SZ(S) % 4; procCycles(SZ(S) - r, S.front() == 'L' or S.front() == 'P'); FOR(i, SZ(S) - r, SZ(S)) moveAny(S[i]); } FOR(i, 0, n) printf("%s\n", T[i]); } int main() { int t = 1; //scanf ("%d", &t); FOR(tid, 1, t+1) { //printf("Case #%d: ", tid); solve(); } return 0; } |