#include <bits/stdc++.h> using namespace std; #define ll long long #define rng(i,a,b) for(int i=int(a);i<int(b);i++) #define rep(i,b) rng(i,0,b) typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<vvl> vvvl; typedef pair<int,int> ii; template<class t> using vc=vector<t>; template<class t> using vvc=vc<vc<t>>; const int MOD = 998244353; ll read(){ ll i; cin>>i; return i; } vi readvi(int n,int off=0,int shift=0){ vi v(n+shift); rep(i,shift)v[i]=0; rep(i,n)v[i+shift]=read()+off; return v; } void YesNo(bool condition, bool do_exit=true) { if (condition) cout << "Yes" << endl; else cout << "No" << endl; if (do_exit) exit(0); } void print(int n, int m, string & s) { rep(i,n) { rep(j, m) cout << s[i*m + j]; cout << endl; } } int main(void ) { ios::sync_with_stdio(false); cin.tie(NULL); int n,m; cin >> n >> m; vector<string> pos(1); vvi adj (1,vi(4,-1)); int dir_to_id[256]; dir_to_id['G'] = 0; dir_to_id['D'] = 1; dir_to_id['L'] = 2; dir_to_id['P'] = 3; string s; rep(i,n) { cin >> s; pos[0] += s; } int num_pos = 1; read(); string moves; cin >> moves; unordered_map<size_t,int> hash_to_pos; hash_to_pos [hash<std::string>{}(pos[0])] = 0; string new_moves; new_moves += moves[0]; char dir_kind[256]; dir_kind['L'] = 'h'; dir_kind['G'] = 'v'; dir_kind['P'] = 'h'; dir_kind['D'] = 'v'; rng(i,1,moves.length()) { char c = moves[i]; if (dir_kind[c] == dir_kind[new_moves.back()]) new_moves.back() = c; else new_moves += c; if (new_moves.length() > 2 and new_moves.back() == new_moves[new_moves.length()-3]) new_moves.pop_back(); } moves = new_moves; int prev_id = 0; for (auto c : moves) { if (adj[prev_id][dir_to_id[c]] != -1) prev_id = adj[prev_id][dir_to_id[c]]; else { string next; switch (c) { case 'L': rep(i, n) { int cnt = 0; rep(j, m) { if (pos[prev_id][i * m + j] != '.') { next += pos[prev_id][i * m + j]; ++cnt; } } rep(j, m - cnt) next += '.'; } break; case 'P': rep(i, n) { int cnt = 0; string row; rep(j, m) { if (pos[prev_id][i * m + (m - 1 - j)] != '.') { row += pos[prev_id][i * m + (m - 1 - j)]; ++cnt; } } rep(j, m - cnt) row += '.'; reverse(row.begin(), row.end()); next += row; } break; case 'G': rep(j, n * m)next += '.'; rep(j, m) { int cnt = 0; rep(i, n) { if (pos[prev_id][i * m + j] != '.') { next[cnt * m + j] = pos[prev_id][i * m + j]; ++cnt; } } } break; case 'D': rep(j, n * m)next += '.'; rep(j, m) { int cnt = n - 1; rep(i, n) { if (pos[prev_id][(n - 1 - i) * m + j] != '.') { next[cnt * m + j] = pos[prev_id][(n - 1 - i) * m + j]; --cnt; } } } break; default: assert(false); } //switch // next == next_conf (new); size_t h = hash<std::string>{}(next); if (hash_to_pos.find(h) == hash_to_pos.end()) { pos.push_back(next); hash_to_pos[h] = num_pos; ++num_pos; adj.push_back(vi(4,-1)); // cerr << num_pos << endl; } adj[prev_id][dir_to_id[c]] = hash_to_pos[h]; prev_id = hash_to_pos[h]; } } print(n,m,pos[prev_id]); 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; #define ll long long #define rng(i,a,b) for(int i=int(a);i<int(b);i++) #define rep(i,b) rng(i,0,b) typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<vvl> vvvl; typedef pair<int,int> ii; template<class t> using vc=vector<t>; template<class t> using vvc=vc<vc<t>>; const int MOD = 998244353; ll read(){ ll i; cin>>i; return i; } vi readvi(int n,int off=0,int shift=0){ vi v(n+shift); rep(i,shift)v[i]=0; rep(i,n)v[i+shift]=read()+off; return v; } void YesNo(bool condition, bool do_exit=true) { if (condition) cout << "Yes" << endl; else cout << "No" << endl; if (do_exit) exit(0); } void print(int n, int m, string & s) { rep(i,n) { rep(j, m) cout << s[i*m + j]; cout << endl; } } int main(void ) { ios::sync_with_stdio(false); cin.tie(NULL); int n,m; cin >> n >> m; vector<string> pos(1); vvi adj (1,vi(4,-1)); int dir_to_id[256]; dir_to_id['G'] = 0; dir_to_id['D'] = 1; dir_to_id['L'] = 2; dir_to_id['P'] = 3; string s; rep(i,n) { cin >> s; pos[0] += s; } int num_pos = 1; read(); string moves; cin >> moves; unordered_map<size_t,int> hash_to_pos; hash_to_pos [hash<std::string>{}(pos[0])] = 0; string new_moves; new_moves += moves[0]; char dir_kind[256]; dir_kind['L'] = 'h'; dir_kind['G'] = 'v'; dir_kind['P'] = 'h'; dir_kind['D'] = 'v'; rng(i,1,moves.length()) { char c = moves[i]; if (dir_kind[c] == dir_kind[new_moves.back()]) new_moves.back() = c; else new_moves += c; if (new_moves.length() > 2 and new_moves.back() == new_moves[new_moves.length()-3]) new_moves.pop_back(); } moves = new_moves; int prev_id = 0; for (auto c : moves) { if (adj[prev_id][dir_to_id[c]] != -1) prev_id = adj[prev_id][dir_to_id[c]]; else { string next; switch (c) { case 'L': rep(i, n) { int cnt = 0; rep(j, m) { if (pos[prev_id][i * m + j] != '.') { next += pos[prev_id][i * m + j]; ++cnt; } } rep(j, m - cnt) next += '.'; } break; case 'P': rep(i, n) { int cnt = 0; string row; rep(j, m) { if (pos[prev_id][i * m + (m - 1 - j)] != '.') { row += pos[prev_id][i * m + (m - 1 - j)]; ++cnt; } } rep(j, m - cnt) row += '.'; reverse(row.begin(), row.end()); next += row; } break; case 'G': rep(j, n * m)next += '.'; rep(j, m) { int cnt = 0; rep(i, n) { if (pos[prev_id][i * m + j] != '.') { next[cnt * m + j] = pos[prev_id][i * m + j]; ++cnt; } } } break; case 'D': rep(j, n * m)next += '.'; rep(j, m) { int cnt = n - 1; rep(i, n) { if (pos[prev_id][(n - 1 - i) * m + j] != '.') { next[cnt * m + j] = pos[prev_id][(n - 1 - i) * m + j]; --cnt; } } } break; default: assert(false); } //switch // next == next_conf (new); size_t h = hash<std::string>{}(next); if (hash_to_pos.find(h) == hash_to_pos.end()) { pos.push_back(next); hash_to_pos[h] = num_pos; ++num_pos; adj.push_back(vi(4,-1)); // cerr << num_pos << endl; } adj[prev_id][dir_to_id[c]] = hash_to_pos[h]; prev_id = hash_to_pos[h]; } } print(n,m,pos[prev_id]); return 0; } |