#include <iostream> #include <cstring> using namespace std; long long pd[500*500], pp[500*500], pa[500 * 500], r[500*500], logp[20][500*500], ppp[4][500 * 500], qqq[4][500 * 500]; long long c[500], w[500]; long long n, k, q, i, ps, mm, m, j,a,b, x,y, lgd, llp, st, sst, rot; char s[502]; char g[500000]; void collect() { for (i = 0; i < n; i++) w[i] = 0; for (j = 0; j < m; j++) c[j] = 0; for (i = 0; i < n; i++) for (j = 0; j < m; j++) if (r[m*i + j] == 0) { w[i] += 1; c[j] += 1; } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); cin >> n; cin >> m; for (i = 0; i < n; i++) { cin >> s; for (j = 0; j < m; j++) { if (s[j] == 'B') { r[m*i + j] = 1; } else if (s[j] == 'C') { r[m*i + j] = -1; } else { r[m*i + j] = 0; } } //printf(s); //printf("\n"); } cin >> k; cin >> g; q = 0; for (mm = 0; mm < 2; mm++) { //printf("%ld k\n", k); while (q < k && (g[q] == 'G' || g[q] == 'D')) q += 1; if (q > 0 && (g[q - 1] == 'G' || g[q - 1] == 'D')) { for (j = 0; j < m; j++) { a = 0; b = 0; while (b < n) { if (r[m*b + j] != 0) { r[m*a + j] = r[m*b + j]; a++; } b++; } for (b = a; b < n; b++) r[m*b + j] = 0; } collect(); //printf("%lld\n", c[3]); lgd = -1; if (g[q - 1] == 'D') { lgd = +1; for (j = 0; j < m; j++) { for (i = n - 1; i >= c[j]; i--) r[i*m + j] = r[(i - c[j])*m + j]; for (i = 0; i < c[j]; i++) r[i*m + j] = 0; } } collect(); } //printf("%ld \n", w[0]); //printf("%ld w\n", w[3]); while (q < k && (g[q] == 'L' || g[q] == 'P')) q += 1; if (q > 0 && (g[q-1]=='L' ||g[q-1]=='P')) { for (i = 0; i < n; i++) { a = 0; b = 0; while (b < m) { if (r[m*i + b] != 0) { r[m*i + a] = r[m*i + b]; a++; } b++; } for (b = a; b < m; b++) r[m*i + b] = 0; } collect(); llp = -1; if (g[q - 1] == 'P') { llp = +1; for (i = 0; i < n; i++) { for (j = m - 1; j >= w[i]; j--) r[i*m + j] = r[i*m + j - w[i]]; for (j = 0; j < w[i]; j++) r[i*m + j] = 0; } } collect(); //printf("%ld k\n", k); } } if (q == k) { for (i = 0; i < n; i++) { for (j = 0; j < m; j++) if (r[i*m + j] == 1) printf("B"); else if (r[i*m + j] == -1) printf("C"); else printf("."); printf("\n"); } } else { //printf("%ld, %ld\n", q,k); //printf("%ld k\n", k); for (i = 0; i < n; i++) for (j = 0; j < m; j++) { pd[i*m + j] = ((i + c[j]) % n)*m + j; pp[i*m + j] = i*m + (j + w[i]) % m; } ps = 0; i = q; if (lgd == -1 && llp == -1) { st = 0; } else if (lgd == -1 && llp == +1) { st = 1; } else if (lgd == +1 && llp == +1) { st = 2; } else { st = 3; } sst = st; if (st == 1 || st == 2) { for (j = 0; j < m/2; j++) { a = c[j]; c[j] = c[m - j - 1]; c[m - j - 1] = a; } } if (st == 2 || st == 3) { for (i = 0; i < n/2; i++) { a = w[i]; w[i] = w[n - i - 1]; w[n - i - 1] = a; } } rot = 0; //printf("%ld sst\n", st); i = q; while (i < k) { //printf("%ld %ld ist\n",st,k); if (g[i] == 'P') { if (st == 0) { st = 1; } else if (st == 3) { st = 2; } } else if (g[i] == 'D') { if (st == 0) { st = 3; rot -= 1; } else if (st == 1) st = 2; } else if (g[i] == 'L') { if (st == 1) { st = 0; } else if (st == 2) { st = 3; } } else { if (st == 3) { st = 0; rot += 1; } else if (st == 2) st = 1; } i += 1; } //printf("%ld\n", sst); //printf("%ld \n", st); for (i = 0; i < n; i++) for (j = 0; j < m; j++) { ppp[0][i*m + j] = i*m + (j + w[i]) % m; qqq[0][i*m + j] = ((i + c[j]) % n)*m + j; ppp[1][i*m + j] = ((i + c[m - j - 1]) % n)*m + j; qqq[1][i*m + j] = i*m + (m + j - w[i]) % m; ppp[2][i*m + j] = i*m + (m + j - w[n - i - 1]) % m; qqq[2][i*m + j] = ((i+n-c[m-j-1])%n)*m + j; ppp[3][i*m + j] = ((i + n - c[j]) % n)*m + j; qqq[3][i*m + j] = i*m + (j + w[n - i - 1]) % m; } if (rot > 0) { for (i = 0; i < n*m; i++) { a = i; for (j = 0; j < 4; j++) a = ppp[j][a]; logp[0][i] = a; } } else if (rot < 0) { for (i = 0; i < n*m; i++) { a = i; for (j = 4; j >0 ; j--) a = qqq[j%4][a]; logp[0][i] = a; } } for (j = 0; j < 19; j++) for (i = 0; i < n*m; i++) logp[j + 1][i] = logp[j][logp[j][i]]; if (rot < 0) rot = -rot; for (i = 0; i < n*m; i++) { pp[i] = i; } j = 0; //printf("%ld ZZZ\n",rot); while (rot > 0) { if (rot % 2 == 1) { for (i = 0; i < n*m; i++) pd[i] = pp[logp[j][i]]; for (i = 0; i < n*m; i++) pp[i] = pd[i]; } rot /= 2; j++; //printf("%ld ZYPZ\n", pp[0]); } //printf("%ld ZYZ\n", pp[0]); for (i = 0; i < n*m; i++) pd[i] = i; for (j = 1; j <= sst; j++) { for (i = 0; i < n*m; i++) pd[i] = pp[qqq[j][i]]; for (i = 0; i < n*m; i++) pp[i] = pd[i]; } //printf("%ld KKK\n",pp[12]); for (j = 0; j < st; j++) { for (i = 0; i < n*m; i++) pd[i] = ppp[j][pp[i]]; for (i = 0; i < n*m; i++) pp[i] = pd[i]; } //printf("%ld KYK\n", pp[12]); for (i = 0; i < n*m; i++) pa[pp[i]] = r[i]; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (pa[i*m + j] == 1) { printf("B"); } else if (pa[i*m + j] == -1){ printf("C"); } else printf("."); } printf("\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 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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | #include <iostream> #include <cstring> using namespace std; long long pd[500*500], pp[500*500], pa[500 * 500], r[500*500], logp[20][500*500], ppp[4][500 * 500], qqq[4][500 * 500]; long long c[500], w[500]; long long n, k, q, i, ps, mm, m, j,a,b, x,y, lgd, llp, st, sst, rot; char s[502]; char g[500000]; void collect() { for (i = 0; i < n; i++) w[i] = 0; for (j = 0; j < m; j++) c[j] = 0; for (i = 0; i < n; i++) for (j = 0; j < m; j++) if (r[m*i + j] == 0) { w[i] += 1; c[j] += 1; } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); cin >> n; cin >> m; for (i = 0; i < n; i++) { cin >> s; for (j = 0; j < m; j++) { if (s[j] == 'B') { r[m*i + j] = 1; } else if (s[j] == 'C') { r[m*i + j] = -1; } else { r[m*i + j] = 0; } } //printf(s); //printf("\n"); } cin >> k; cin >> g; q = 0; for (mm = 0; mm < 2; mm++) { //printf("%ld k\n", k); while (q < k && (g[q] == 'G' || g[q] == 'D')) q += 1; if (q > 0 && (g[q - 1] == 'G' || g[q - 1] == 'D')) { for (j = 0; j < m; j++) { a = 0; b = 0; while (b < n) { if (r[m*b + j] != 0) { r[m*a + j] = r[m*b + j]; a++; } b++; } for (b = a; b < n; b++) r[m*b + j] = 0; } collect(); //printf("%lld\n", c[3]); lgd = -1; if (g[q - 1] == 'D') { lgd = +1; for (j = 0; j < m; j++) { for (i = n - 1; i >= c[j]; i--) r[i*m + j] = r[(i - c[j])*m + j]; for (i = 0; i < c[j]; i++) r[i*m + j] = 0; } } collect(); } //printf("%ld \n", w[0]); //printf("%ld w\n", w[3]); while (q < k && (g[q] == 'L' || g[q] == 'P')) q += 1; if (q > 0 && (g[q-1]=='L' ||g[q-1]=='P')) { for (i = 0; i < n; i++) { a = 0; b = 0; while (b < m) { if (r[m*i + b] != 0) { r[m*i + a] = r[m*i + b]; a++; } b++; } for (b = a; b < m; b++) r[m*i + b] = 0; } collect(); llp = -1; if (g[q - 1] == 'P') { llp = +1; for (i = 0; i < n; i++) { for (j = m - 1; j >= w[i]; j--) r[i*m + j] = r[i*m + j - w[i]]; for (j = 0; j < w[i]; j++) r[i*m + j] = 0; } } collect(); //printf("%ld k\n", k); } } if (q == k) { for (i = 0; i < n; i++) { for (j = 0; j < m; j++) if (r[i*m + j] == 1) printf("B"); else if (r[i*m + j] == -1) printf("C"); else printf("."); printf("\n"); } } else { //printf("%ld, %ld\n", q,k); //printf("%ld k\n", k); for (i = 0; i < n; i++) for (j = 0; j < m; j++) { pd[i*m + j] = ((i + c[j]) % n)*m + j; pp[i*m + j] = i*m + (j + w[i]) % m; } ps = 0; i = q; if (lgd == -1 && llp == -1) { st = 0; } else if (lgd == -1 && llp == +1) { st = 1; } else if (lgd == +1 && llp == +1) { st = 2; } else { st = 3; } sst = st; if (st == 1 || st == 2) { for (j = 0; j < m/2; j++) { a = c[j]; c[j] = c[m - j - 1]; c[m - j - 1] = a; } } if (st == 2 || st == 3) { for (i = 0; i < n/2; i++) { a = w[i]; w[i] = w[n - i - 1]; w[n - i - 1] = a; } } rot = 0; //printf("%ld sst\n", st); i = q; while (i < k) { //printf("%ld %ld ist\n",st,k); if (g[i] == 'P') { if (st == 0) { st = 1; } else if (st == 3) { st = 2; } } else if (g[i] == 'D') { if (st == 0) { st = 3; rot -= 1; } else if (st == 1) st = 2; } else if (g[i] == 'L') { if (st == 1) { st = 0; } else if (st == 2) { st = 3; } } else { if (st == 3) { st = 0; rot += 1; } else if (st == 2) st = 1; } i += 1; } //printf("%ld\n", sst); //printf("%ld \n", st); for (i = 0; i < n; i++) for (j = 0; j < m; j++) { ppp[0][i*m + j] = i*m + (j + w[i]) % m; qqq[0][i*m + j] = ((i + c[j]) % n)*m + j; ppp[1][i*m + j] = ((i + c[m - j - 1]) % n)*m + j; qqq[1][i*m + j] = i*m + (m + j - w[i]) % m; ppp[2][i*m + j] = i*m + (m + j - w[n - i - 1]) % m; qqq[2][i*m + j] = ((i+n-c[m-j-1])%n)*m + j; ppp[3][i*m + j] = ((i + n - c[j]) % n)*m + j; qqq[3][i*m + j] = i*m + (j + w[n - i - 1]) % m; } if (rot > 0) { for (i = 0; i < n*m; i++) { a = i; for (j = 0; j < 4; j++) a = ppp[j][a]; logp[0][i] = a; } } else if (rot < 0) { for (i = 0; i < n*m; i++) { a = i; for (j = 4; j >0 ; j--) a = qqq[j%4][a]; logp[0][i] = a; } } for (j = 0; j < 19; j++) for (i = 0; i < n*m; i++) logp[j + 1][i] = logp[j][logp[j][i]]; if (rot < 0) rot = -rot; for (i = 0; i < n*m; i++) { pp[i] = i; } j = 0; //printf("%ld ZZZ\n",rot); while (rot > 0) { if (rot % 2 == 1) { for (i = 0; i < n*m; i++) pd[i] = pp[logp[j][i]]; for (i = 0; i < n*m; i++) pp[i] = pd[i]; } rot /= 2; j++; //printf("%ld ZYPZ\n", pp[0]); } //printf("%ld ZYZ\n", pp[0]); for (i = 0; i < n*m; i++) pd[i] = i; for (j = 1; j <= sst; j++) { for (i = 0; i < n*m; i++) pd[i] = pp[qqq[j][i]]; for (i = 0; i < n*m; i++) pp[i] = pd[i]; } //printf("%ld KKK\n",pp[12]); for (j = 0; j < st; j++) { for (i = 0; i < n*m; i++) pd[i] = ppp[j][pp[i]]; for (i = 0; i < n*m; i++) pp[i] = pd[i]; } //printf("%ld KYK\n", pp[12]); for (i = 0; i < n*m; i++) pa[pp[i]] = r[i]; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (pa[i*m + j] == 1) { printf("B"); } else if (pa[i*m + j] == -1){ printf("C"); } else printf("."); } printf("\n"); } } } |