#include <bits/stdc++.h> using namespace std; using LL = long long; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define ssize(x) int(x.size()) template<class A, class B> auto &operator<<(ostream &o, pair<A, B> p) { return o << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) { o << '{'; int i = 0; for (auto e : x) o << (", ")+2*!i++ << e; return o << '}'; } #ifdef DEBUG #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'; #else #define debug(...) {} #endif constexpr int MAXN = 513; constexpr int LOGN = 19; pair<int, int> jmp[MAXN][MAXN][LOGN]; pair<int, int> orig[MAXN][MAXN][5]; pair<int, int> revg[MAXN][MAXN][4]; char g[MAXN][MAXN]; int n, m; void mov(int sx, int sy, int dx, int dy, char dir, int lay) { REP(i, n) { REP(j, m) { if (g[sx][sy] != '.') { int x = sx, y = sy; while (g[x + dx][y + dy] == '.') { swap(g[x][y], g[x + dx][y + dy]); swap(orig[x][y][lay], orig[x + dx][y + dy][lay]); x += dx; y += dy; } } if (dir == 'L' || dir == 'G') { sy++; if (sy == m + 1) { sx++; sy = 1; } } else { sy--; if (sy == 0) { sx--; sy = m; } } } } } void run_mov(char dir, int lay = 0) { if (dir == 'L') { mov(1, 1, 0, -1, 'L', lay); } else if (dir == 'G') { mov(1, 1, -1, 0, 'G', lay); } else if (dir == 'P') { mov(n, m, 0, 1, 'P', lay); } else { mov(n, m, 1, 0, 'D', lay); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int l; string s; cin >> n >> m; FOR(i, 1, n) { FOR(j, 1, m) cin >> g[i][j]; } FOR(i, 0, n + 1) g[i][0] = g[i][m + 1] = '#'; FOR(j, 0, m + 1) g[0][j] = g[n + 1][j] = '#'; cin >> l >> s; vector<char> moves; vector<char> opposite(313); opposite['L'] = 'P'; opposite['P'] = 'L'; opposite['G'] = 'D'; opposite['D'] = 'G'; vector<bool> in(313), vis(313); vector<bool> fst(l + 1); fst[0] = true; REP(i, l) { if (!vis[s[i]]) { vis[s[i]] = true; moves.emplace_back(s[i]); fst[ssize(moves)] = true; in[s[i]] = true; in[opposite[s[i]]] = false; } else { if (in[s[i]]) continue; if (!fst[ssize(moves)] && moves.back() == opposite[s[i]]) { moves.pop_back(); } else { moves.emplace_back(s[i]); } in[s[i]] = true; in[opposite[s[i]]] = false; } } l = ssize(moves); for (int i = 0; i < min(8, l); i++) run_mov(moves[i]); if (l < 12) { FOR(i, 8, l - 1) run_mov(moves[i]); FOR(i, 1, n) { FOR(j, 1, m) cout << g[i][j]; cout << '\n'; } return 0; } REP(i, n + 2) { REP(j, m + 2) { orig[i][j][0] = {i, j}; } } FOR(lay, 1, 4) { REP(i, n + 2) { REP(j, m + 2) orig[i][j][lay] = orig[i][j][lay - 1]; } run_mov(moves[lay + 7], lay); } FOR(i, 1, n) { FOR(j, 1, m) { REP(k, 4) { auto &[x, y] = orig[i][j][k]; revg[x][y][k] = {i, j}; } auto &[x, y] = orig[i][j][4]; jmp[x][y][2] = {i, j}; } } for (int lay = 3; lay < LOGN; lay++) { FOR(i, 1, n) { FOR(j, 1, m) { auto &[x, y] = jmp[i][j][lay - 1]; jmp[i][j][lay] = jmp[x][y][lay - 1]; } } } vector<vector<char> > ans(n + 2, vector<char>(m + 2, '.')); l -= 12; FOR(i, 1, n) { FOR(j, 1, m) { int curr_l = l; int x = i, y = j; for (int lay = LOGN - 1; lay >= 2; lay--) { if (curr_l >= (1 << lay)) { curr_l -= (1 << lay); auto [xx, yy] = jmp[x][y][lay]; x = xx; y = yy; } } auto [xx, yy] = revg[x][y][curr_l % 4]; ans[xx][yy] = g[i][j]; } } FOR(i, 1, n) { FOR(j, 1, m) cout << ans[i][j]; cout << '\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 181 182 183 184 185 186 187 188 189 190 191 192 193 | #include <bits/stdc++.h> using namespace std; using LL = long long; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define ssize(x) int(x.size()) template<class A, class B> auto &operator<<(ostream &o, pair<A, B> p) { return o << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) { o << '{'; int i = 0; for (auto e : x) o << (", ")+2*!i++ << e; return o << '}'; } #ifdef DEBUG #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'; #else #define debug(...) {} #endif constexpr int MAXN = 513; constexpr int LOGN = 19; pair<int, int> jmp[MAXN][MAXN][LOGN]; pair<int, int> orig[MAXN][MAXN][5]; pair<int, int> revg[MAXN][MAXN][4]; char g[MAXN][MAXN]; int n, m; void mov(int sx, int sy, int dx, int dy, char dir, int lay) { REP(i, n) { REP(j, m) { if (g[sx][sy] != '.') { int x = sx, y = sy; while (g[x + dx][y + dy] == '.') { swap(g[x][y], g[x + dx][y + dy]); swap(orig[x][y][lay], orig[x + dx][y + dy][lay]); x += dx; y += dy; } } if (dir == 'L' || dir == 'G') { sy++; if (sy == m + 1) { sx++; sy = 1; } } else { sy--; if (sy == 0) { sx--; sy = m; } } } } } void run_mov(char dir, int lay = 0) { if (dir == 'L') { mov(1, 1, 0, -1, 'L', lay); } else if (dir == 'G') { mov(1, 1, -1, 0, 'G', lay); } else if (dir == 'P') { mov(n, m, 0, 1, 'P', lay); } else { mov(n, m, 1, 0, 'D', lay); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int l; string s; cin >> n >> m; FOR(i, 1, n) { FOR(j, 1, m) cin >> g[i][j]; } FOR(i, 0, n + 1) g[i][0] = g[i][m + 1] = '#'; FOR(j, 0, m + 1) g[0][j] = g[n + 1][j] = '#'; cin >> l >> s; vector<char> moves; vector<char> opposite(313); opposite['L'] = 'P'; opposite['P'] = 'L'; opposite['G'] = 'D'; opposite['D'] = 'G'; vector<bool> in(313), vis(313); vector<bool> fst(l + 1); fst[0] = true; REP(i, l) { if (!vis[s[i]]) { vis[s[i]] = true; moves.emplace_back(s[i]); fst[ssize(moves)] = true; in[s[i]] = true; in[opposite[s[i]]] = false; } else { if (in[s[i]]) continue; if (!fst[ssize(moves)] && moves.back() == opposite[s[i]]) { moves.pop_back(); } else { moves.emplace_back(s[i]); } in[s[i]] = true; in[opposite[s[i]]] = false; } } l = ssize(moves); for (int i = 0; i < min(8, l); i++) run_mov(moves[i]); if (l < 12) { FOR(i, 8, l - 1) run_mov(moves[i]); FOR(i, 1, n) { FOR(j, 1, m) cout << g[i][j]; cout << '\n'; } return 0; } REP(i, n + 2) { REP(j, m + 2) { orig[i][j][0] = {i, j}; } } FOR(lay, 1, 4) { REP(i, n + 2) { REP(j, m + 2) orig[i][j][lay] = orig[i][j][lay - 1]; } run_mov(moves[lay + 7], lay); } FOR(i, 1, n) { FOR(j, 1, m) { REP(k, 4) { auto &[x, y] = orig[i][j][k]; revg[x][y][k] = {i, j}; } auto &[x, y] = orig[i][j][4]; jmp[x][y][2] = {i, j}; } } for (int lay = 3; lay < LOGN; lay++) { FOR(i, 1, n) { FOR(j, 1, m) { auto &[x, y] = jmp[i][j][lay - 1]; jmp[i][j][lay] = jmp[x][y][lay - 1]; } } } vector<vector<char> > ans(n + 2, vector<char>(m + 2, '.')); l -= 12; FOR(i, 1, n) { FOR(j, 1, m) { int curr_l = l; int x = i, y = j; for (int lay = LOGN - 1; lay >= 2; lay--) { if (curr_l >= (1 << lay)) { curr_l -= (1 << lay); auto [xx, yy] = jmp[x][y][lay]; x = xx; y = yy; } } auto [xx, yy] = revg[x][y][curr_l % 4]; ans[xx][yy] = g[i][j]; } } FOR(i, 1, n) { FOR(j, 1, m) cout << ans[i][j]; cout << '\n'; } return 0; } |