#include<cstdio> #include<iostream> #include<iomanip> #include<algorithm> #include<string> #include<vector> #include<cmath> #include<set> #include<unordered_map> using namespace std; typedef vector<int> VI; typedef long long LL; typedef vector<LL> VLL; typedef pair<int, int> PII; typedef vector<PII> VPII; #define FOR(x, b, e) for(int x=b; x<=(e); ++x) #define FORD(x, b, e) for(int x=b; x>=(e); --x) #define REP(x, n) for(int x=0; x<(n); ++x) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define PB push_back #define ST first #define ND second void move(vector<string> &b, char mv) { if (mv == 'L' || mv == 'P') { REP(i, b.size()) { int j, d; if (mv == 'P') { j = b[i].size() - 1; d = -1; } else { j = 0; d = 1; } while (j >=0 && j < b[i].size() && b[i][j] != '.') j += d; int k = j + d; while (k >= 0 && k < b[i].size()) { if (b[i][k] != '.') { swap(b[i][j], b[i][k]); j += d; } k += d; } } } else { REP(i, b[0].size()) { int j, d; if (mv == 'D') { j = b.size() - 1; d = -1; } else { j = 0; d = 1; } while (j >=0 && j < b.size() && b[j][i] != '.') j += d; int k = j + d; while (k >= 0 && k < b.size()) { if (b[k][i] != '.') { swap(b[j][i], b[k][i]); j += d; } k += d; } } } } void print(vector<string> &b) { for (string s : b) cout << s << '\n'; } bool sameAxis(char &a, char &b) { return (a != 'L' && b != 'L' && a != 'P' && b != 'P') || (a != 'D' && b != 'D' && a != 'G' && b != 'G'); } bool isBack(char &last, char &next) { return sameAxis(last, next) && last != next; } bool isForward(string &st, char &next) { if (st.size() ==1) return !sameAxis(st[0], next); return !sameAxis(st[st.size() - 1], next) && st[st.size() - 2] != next; } bool equals(vector<string> &a, vector<string> &b) { REP(i, a.size()) REP(j, a[i].size()) if (a[i] != b[i]) return false; return true; } void calculateColumns(vector<string> &b, VI &col, bool isLeft) { REP(x, col.size()) { int i = 0, j = b.size() - 1; while (i <= j && b[i][x] == '.') i++; while (i <= j && b[j][x] == '.') j--; col[isLeft ? col.size() - x - 1 : x] = j - i + 1; } } void calculateRows(vector<string> &b, VI &rows, bool isTop) { REP(x, rows.size()) { int i = 0, j = b[x].size() - 1; while (i <= j && b[x][i] == '.') i++; while (i <= j && b[x][j] == '.') j--; rows[isTop ? x : rows.size() - x - 1] = j - i + 1; } } void proceed(PII &loc, char mv, bool &isTop, bool &isLeft, VI &columns, VI &rows) { //printf("move %c, left: %d, top: %d, loc: {%d, %d}\n", mv, isLeft, isTop, loc.ST, loc.ND); if (mv == 'L') { loc.ND -= columns.size() - (isTop ? rows[loc.ST] : rows[rows.size() - 1 - loc.ST]); isLeft = true; } else if (mv == 'P') { loc.ND += columns.size() - (isTop ? rows[loc.ST] : rows[rows.size() - 1 - loc.ST]); isLeft = false; } else if (mv == 'G') { loc.ST -= rows.size() - (isLeft ? columns[columns.size() - 1 - loc.ND] : columns[loc.ND]); isTop = true; } else { loc.ST += rows.size() - (isLeft ? columns[columns.size() - 1 - loc.ND] : columns[loc.ND]); isTop = false; } //printf("done, loc: {%d, %d}\n", loc.ST, loc.ND); } void print(VI &x) { for (int a : x) cout << a << ' '; cout << '\n'; } unordered_map<int, vector<VPII> > mem; vector<VPII> calculateJumps(vector<string> &board, string &reduced, int &p, VI &columns, VI &rows) { bool isTop = reduced[0] == 'G' || reduced[1] == 'G'; bool isLeft = reduced[0] == 'L' || reduced[1] == 'L'; int length = 4; vector<VPII> jumps(board.size(), VPII(board[0].size())); REP(i, board.size()) REP(j, board[i].size()) { if (board[i][j] == '.') continue; PII current = {i, j}; char firstMv = reduced[p]; isTop = reduced[0] == 'G' || reduced[1] == 'G'; isLeft = reduced[0] == 'L' || reduced[1] == 'L'; REP(j, 4) { proceed(current, reduced[p + j], isTop, isLeft, columns, rows); } jumps[i][j] = {current.ST, current.ND}; } length *= 2; vector<VPII> tmp(jumps.size(), VPII(jumps[0].size())); while (p + length <= reduced.size()) { //printf("length: %d\n", length); REP(i, board.size()) REP(j, board[i].size()) { if (board[i][j] == '.') continue; tmp[i][j] = jumps[jumps[i][j].ST][jumps[i][j].ND]; } swap(jumps, tmp); length *= 2; } p += length / 2; return jumps; } void applyJumps(vector<string> &board, vector<string> &res, vector<VPII> &jumps) { REP(i, board.size()) REP(j, board[i].size()) { if (board[i][j] == '.') continue; res[jumps[i][j].ST][jumps[i][j].ND] = board[i][j]; } } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; vector<string> board(n); REP(i, n) cin >> board[i]; int k; string moves; cin >> k >> moves; string reduced; REP(i, k) { if (reduced.empty()) reduced.PB(moves[i]); else if (isBack(reduced[reduced.size() - 1], moves[i])) { reduced.pop_back(); if (reduced.empty() || isForward(reduced, moves[i])) reduced.PB(moves[i]); } else if (isForward(reduced, moves[i])) reduced.PB(moves[i]); } if (reduced.size() < 10) { REP(i, reduced.size()) { move(board, reduced[i]); } print(board); } else { move(board, reduced[0]); move(board, reduced[1]); VI columns(m), rows(n); bool isTop = reduced[0] == 'G' || reduced[1] == 'G'; bool isLeft = reduced[0] == 'L' || reduced[1] == 'L'; calculateColumns(board, columns, isLeft); calculateRows(board, rows, isTop); int p = 2; //cout << reduced << '\n' << flush; vector<string> result(n, string(m, '.')); while (p + 4 <= reduced.size()) { vector<VPII> jumps = calculateJumps(board, reduced, p, columns, rows); //printf("jumps generated, p = %d\n", p); applyJumps(board, result, jumps); swap(board, result); } while (p < reduced.size()) { move(board, reduced[p]); p++; } print(board); } 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 214 215 | #include<cstdio> #include<iostream> #include<iomanip> #include<algorithm> #include<string> #include<vector> #include<cmath> #include<set> #include<unordered_map> using namespace std; typedef vector<int> VI; typedef long long LL; typedef vector<LL> VLL; typedef pair<int, int> PII; typedef vector<PII> VPII; #define FOR(x, b, e) for(int x=b; x<=(e); ++x) #define FORD(x, b, e) for(int x=b; x>=(e); --x) #define REP(x, n) for(int x=0; x<(n); ++x) #define VAR(v, n) __typeof(n) v = (n) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define PB push_back #define ST first #define ND second void move(vector<string> &b, char mv) { if (mv == 'L' || mv == 'P') { REP(i, b.size()) { int j, d; if (mv == 'P') { j = b[i].size() - 1; d = -1; } else { j = 0; d = 1; } while (j >=0 && j < b[i].size() && b[i][j] != '.') j += d; int k = j + d; while (k >= 0 && k < b[i].size()) { if (b[i][k] != '.') { swap(b[i][j], b[i][k]); j += d; } k += d; } } } else { REP(i, b[0].size()) { int j, d; if (mv == 'D') { j = b.size() - 1; d = -1; } else { j = 0; d = 1; } while (j >=0 && j < b.size() && b[j][i] != '.') j += d; int k = j + d; while (k >= 0 && k < b.size()) { if (b[k][i] != '.') { swap(b[j][i], b[k][i]); j += d; } k += d; } } } } void print(vector<string> &b) { for (string s : b) cout << s << '\n'; } bool sameAxis(char &a, char &b) { return (a != 'L' && b != 'L' && a != 'P' && b != 'P') || (a != 'D' && b != 'D' && a != 'G' && b != 'G'); } bool isBack(char &last, char &next) { return sameAxis(last, next) && last != next; } bool isForward(string &st, char &next) { if (st.size() ==1) return !sameAxis(st[0], next); return !sameAxis(st[st.size() - 1], next) && st[st.size() - 2] != next; } bool equals(vector<string> &a, vector<string> &b) { REP(i, a.size()) REP(j, a[i].size()) if (a[i] != b[i]) return false; return true; } void calculateColumns(vector<string> &b, VI &col, bool isLeft) { REP(x, col.size()) { int i = 0, j = b.size() - 1; while (i <= j && b[i][x] == '.') i++; while (i <= j && b[j][x] == '.') j--; col[isLeft ? col.size() - x - 1 : x] = j - i + 1; } } void calculateRows(vector<string> &b, VI &rows, bool isTop) { REP(x, rows.size()) { int i = 0, j = b[x].size() - 1; while (i <= j && b[x][i] == '.') i++; while (i <= j && b[x][j] == '.') j--; rows[isTop ? x : rows.size() - x - 1] = j - i + 1; } } void proceed(PII &loc, char mv, bool &isTop, bool &isLeft, VI &columns, VI &rows) { //printf("move %c, left: %d, top: %d, loc: {%d, %d}\n", mv, isLeft, isTop, loc.ST, loc.ND); if (mv == 'L') { loc.ND -= columns.size() - (isTop ? rows[loc.ST] : rows[rows.size() - 1 - loc.ST]); isLeft = true; } else if (mv == 'P') { loc.ND += columns.size() - (isTop ? rows[loc.ST] : rows[rows.size() - 1 - loc.ST]); isLeft = false; } else if (mv == 'G') { loc.ST -= rows.size() - (isLeft ? columns[columns.size() - 1 - loc.ND] : columns[loc.ND]); isTop = true; } else { loc.ST += rows.size() - (isLeft ? columns[columns.size() - 1 - loc.ND] : columns[loc.ND]); isTop = false; } //printf("done, loc: {%d, %d}\n", loc.ST, loc.ND); } void print(VI &x) { for (int a : x) cout << a << ' '; cout << '\n'; } unordered_map<int, vector<VPII> > mem; vector<VPII> calculateJumps(vector<string> &board, string &reduced, int &p, VI &columns, VI &rows) { bool isTop = reduced[0] == 'G' || reduced[1] == 'G'; bool isLeft = reduced[0] == 'L' || reduced[1] == 'L'; int length = 4; vector<VPII> jumps(board.size(), VPII(board[0].size())); REP(i, board.size()) REP(j, board[i].size()) { if (board[i][j] == '.') continue; PII current = {i, j}; char firstMv = reduced[p]; isTop = reduced[0] == 'G' || reduced[1] == 'G'; isLeft = reduced[0] == 'L' || reduced[1] == 'L'; REP(j, 4) { proceed(current, reduced[p + j], isTop, isLeft, columns, rows); } jumps[i][j] = {current.ST, current.ND}; } length *= 2; vector<VPII> tmp(jumps.size(), VPII(jumps[0].size())); while (p + length <= reduced.size()) { //printf("length: %d\n", length); REP(i, board.size()) REP(j, board[i].size()) { if (board[i][j] == '.') continue; tmp[i][j] = jumps[jumps[i][j].ST][jumps[i][j].ND]; } swap(jumps, tmp); length *= 2; } p += length / 2; return jumps; } void applyJumps(vector<string> &board, vector<string> &res, vector<VPII> &jumps) { REP(i, board.size()) REP(j, board[i].size()) { if (board[i][j] == '.') continue; res[jumps[i][j].ST][jumps[i][j].ND] = board[i][j]; } } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; vector<string> board(n); REP(i, n) cin >> board[i]; int k; string moves; cin >> k >> moves; string reduced; REP(i, k) { if (reduced.empty()) reduced.PB(moves[i]); else if (isBack(reduced[reduced.size() - 1], moves[i])) { reduced.pop_back(); if (reduced.empty() || isForward(reduced, moves[i])) reduced.PB(moves[i]); } else if (isForward(reduced, moves[i])) reduced.PB(moves[i]); } if (reduced.size() < 10) { REP(i, reduced.size()) { move(board, reduced[i]); } print(board); } else { move(board, reduced[0]); move(board, reduced[1]); VI columns(m), rows(n); bool isTop = reduced[0] == 'G' || reduced[1] == 'G'; bool isLeft = reduced[0] == 'L' || reduced[1] == 'L'; calculateColumns(board, columns, isLeft); calculateRows(board, rows, isTop); int p = 2; //cout << reduced << '\n' << flush; vector<string> result(n, string(m, '.')); while (p + 4 <= reduced.size()) { vector<VPII> jumps = calculateJumps(board, reduced, p, columns, rows); //printf("jumps generated, p = %d\n", p); applyJumps(board, result, jumps); swap(board, result); } while (p < reduced.size()) { move(board, reduced[p]); p++; } print(board); } return 0; } |