#include <bits/stdc++.h> using namespace std; #define UP 'G' #define DOWN 'D' #define LEFT 'L' #define RIGHT 'P' #define EMPTY '.' #define BLACK 'C' #define WHITE 'B' using ull = unsigned long long; void print_grid(vector<vector<int>> &grid, vector<char> &mapping) { int h = grid.size(); int w = grid[0].size(); for (int row = 0; row < h; row++) { for (int col = 0; col < w; col++) { int v = grid[row][col]; cout << mapping[v]; // cout << v; } cout << '\n'; } cout.flush(); } bool is_vertical(char c) { return (c == UP) || (c == DOWN); } void fix(string &s) { int n; while ((n = s.size()) > 1) { // cerr << '.'; // cerr << s << endl; if (s[n - 1] == s[n - 2]) s.pop_back(); else if (is_vertical(s[n - 1]) == is_vertical(s[n - 2])) { s[n - 2] = s[n - 1]; s.pop_back(); } else if (s.size() > 2 && s[n - 3] == s[n - 1]) s.pop_back(); else break; } } string normalize(const string &s) { string res; for (char c : s) { res.push_back(c); fix(res); } return res; } void apply_perm(vector<vector<int>> &grid, vector<vector<int>> &cycles, int times) { vector<int> perm(1, 0); for (auto &cycle : cycles) { int n = cycle.size(); for (int i = 0; i < n; i++) { while (int(perm.size()) < cycle[i] + 1) perm.emplace_back(); perm[cycle[i]] = cycle[(i + times) % cycle.size()]; } } int h = grid.size(); int w = grid[0].size(); for (int row = 0; row < h; row++) { for (int col = 0; col < w; col++) { grid[row][col] = perm[grid[row][col]]; } } } void onemove(vector<vector<int>> &grid, char dir) { // cerr << "doing " << dir << endl; int h = grid.size(); int w = grid[0].size(); if (dir == UP) { for (int col = 0; col < w; col++) { int writerow = 0; for (int row = 0; row < h; row++) { int c = grid[row][col]; if (!c) continue; else { grid[writerow++][col] = c; } } for (int row = writerow; row < h; row++) { grid[row][col] = 0; } } } else if (dir == DOWN) { for (int col = 0; col < w; col++) { int writerow = h - 1; for (int row = h - 1; row >= 0; row--) { int c = grid[row][col]; if (!c) continue; else { grid[writerow--][col] = c; } } for (int row = writerow; row >= 0; row--) { grid[row][col] = 0; } } } else if (dir == LEFT) { for (int row = 0; row < h; row++) { int writecol = 0; for (int col = 0; col < w; col++) { int c = grid[row][col]; if (!c) continue; else { grid[row][writecol++] = c; } } for (int col = writecol; col < w; col++) { grid[row][col] = 0; } } } else { for (int row = 0; row < h; row++) { int writecol = w - 1; for (int col = w - 1; col >= 0; col--) { int c = grid[row][col]; if (!c) continue; else { grid[row][writecol--] = c; } } for (int col = writecol; col >= 0; col--) { grid[row][col] = 0; } } } // seen.insert(grid_hash(grid)); // print_grid(grid); } vector<vector<int>> get_perm(vector<vector<int>> &grid, string rep) { // print_grid(grid, mapping); vector<vector<int>> ngrid = grid; vector<int> perm(1, 0); for (char c: rep) onemove(ngrid, c); // cerr << "did onemoves" << endl; int h = grid.size(); int w = grid[0].size(); for (int row = 0; row < h; row++) { for (int col = 0; col < w; col++) { int v = grid[row][col]; while (int(perm.size()) < v + 1) perm.emplace_back(); perm[v] = ngrid[row][col]; } } // print_grid(ngrid, mapping); // cerr << "perm" << endl; // for (int v : perm) cerr << v << ' '; // cerr << endl; vector<vector<int>> cycles; for (int i = 0; i < int(perm.size()); i++) { int target = perm[i]; if (target == -1) continue; cycles.emplace_back(); while (true) { cycles.back().push_back(target); int next_target = perm[target]; perm[target] = -1; if (target == i) break; target = next_target; } } return cycles; } void solve() { int h, w; cin >> h >> w; vector<vector<int>> grid(h, vector<int>(w)); int which = 1; vector<char> mapping; mapping.push_back('.'); for (int row = 0; row < h; row++) { for (int col = 0; col < w; col++) { char c; cin >> c; if (c == '.') grid[row][col] = 0; else { grid[row][col] = which++; mapping.push_back(c); } } } int k; cin >> k; string s; cin >> s; // cout << s << endl; // cout << "~~~~~~" << endl; // onemove(grid, s[0]); int i = 1; while (i < k) { if (is_vertical(s[i]) != is_vertical(s[i - 1])) break; i++; } onemove(grid, s[i - 1]); i++; while (i < k) { if (is_vertical(s[i]) != is_vertical(s[i - 1])) break; i++; } if (i <= k) onemove(grid, s[i - 1]); if (i >= k) { print_grid(grid, mapping); return; } s = s.substr(i, string::npos); // // cerr << s << endl; // cerr << s << endl; s = normalize(s); // cerr << s << endl; if (s.size() >= 4 + s.size() % 4) { for (int j = 0; j < int(4 + s.size() % 4); j++) onemove(grid, s[j]); string rep = s.substr(s.size() - 4, string::npos); // cerr << rep << endl; auto v = get_perm(grid, rep); apply_perm(grid, v, (s.size() / 4) - 1); // s.resize(s.size() % 4); } else { for (char c : s) onemove(grid, c); } // cerr << s << endl; // int new_size = 120 + s.size() % 120; // if (new_size < int(s.size())) { // s.resize(new_size); // } // print_grid(grid); // onemove(grid, s[0]); // if (s.size() > 0) { // onemove(grid, s[1]); // } // if (s.size() > 4) { // onemove(grid, s[s.size() - 2]); // } // if (s.size() > 3) { // onemove(grid, s[s.size() - 1]); // } // char c; // while (cin >> c) onemove(grid, c); // cout << seen.size() << endl; print_grid(grid, mapping); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); // #ifdef LOCAL // int t; cin >> t; while (t--) // #endif solve(); cout.flush(); }
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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | #include <bits/stdc++.h> using namespace std; #define UP 'G' #define DOWN 'D' #define LEFT 'L' #define RIGHT 'P' #define EMPTY '.' #define BLACK 'C' #define WHITE 'B' using ull = unsigned long long; void print_grid(vector<vector<int>> &grid, vector<char> &mapping) { int h = grid.size(); int w = grid[0].size(); for (int row = 0; row < h; row++) { for (int col = 0; col < w; col++) { int v = grid[row][col]; cout << mapping[v]; // cout << v; } cout << '\n'; } cout.flush(); } bool is_vertical(char c) { return (c == UP) || (c == DOWN); } void fix(string &s) { int n; while ((n = s.size()) > 1) { // cerr << '.'; // cerr << s << endl; if (s[n - 1] == s[n - 2]) s.pop_back(); else if (is_vertical(s[n - 1]) == is_vertical(s[n - 2])) { s[n - 2] = s[n - 1]; s.pop_back(); } else if (s.size() > 2 && s[n - 3] == s[n - 1]) s.pop_back(); else break; } } string normalize(const string &s) { string res; for (char c : s) { res.push_back(c); fix(res); } return res; } void apply_perm(vector<vector<int>> &grid, vector<vector<int>> &cycles, int times) { vector<int> perm(1, 0); for (auto &cycle : cycles) { int n = cycle.size(); for (int i = 0; i < n; i++) { while (int(perm.size()) < cycle[i] + 1) perm.emplace_back(); perm[cycle[i]] = cycle[(i + times) % cycle.size()]; } } int h = grid.size(); int w = grid[0].size(); for (int row = 0; row < h; row++) { for (int col = 0; col < w; col++) { grid[row][col] = perm[grid[row][col]]; } } } void onemove(vector<vector<int>> &grid, char dir) { // cerr << "doing " << dir << endl; int h = grid.size(); int w = grid[0].size(); if (dir == UP) { for (int col = 0; col < w; col++) { int writerow = 0; for (int row = 0; row < h; row++) { int c = grid[row][col]; if (!c) continue; else { grid[writerow++][col] = c; } } for (int row = writerow; row < h; row++) { grid[row][col] = 0; } } } else if (dir == DOWN) { for (int col = 0; col < w; col++) { int writerow = h - 1; for (int row = h - 1; row >= 0; row--) { int c = grid[row][col]; if (!c) continue; else { grid[writerow--][col] = c; } } for (int row = writerow; row >= 0; row--) { grid[row][col] = 0; } } } else if (dir == LEFT) { for (int row = 0; row < h; row++) { int writecol = 0; for (int col = 0; col < w; col++) { int c = grid[row][col]; if (!c) continue; else { grid[row][writecol++] = c; } } for (int col = writecol; col < w; col++) { grid[row][col] = 0; } } } else { for (int row = 0; row < h; row++) { int writecol = w - 1; for (int col = w - 1; col >= 0; col--) { int c = grid[row][col]; if (!c) continue; else { grid[row][writecol--] = c; } } for (int col = writecol; col >= 0; col--) { grid[row][col] = 0; } } } // seen.insert(grid_hash(grid)); // print_grid(grid); } vector<vector<int>> get_perm(vector<vector<int>> &grid, string rep) { // print_grid(grid, mapping); vector<vector<int>> ngrid = grid; vector<int> perm(1, 0); for (char c: rep) onemove(ngrid, c); // cerr << "did onemoves" << endl; int h = grid.size(); int w = grid[0].size(); for (int row = 0; row < h; row++) { for (int col = 0; col < w; col++) { int v = grid[row][col]; while (int(perm.size()) < v + 1) perm.emplace_back(); perm[v] = ngrid[row][col]; } } // print_grid(ngrid, mapping); // cerr << "perm" << endl; // for (int v : perm) cerr << v << ' '; // cerr << endl; vector<vector<int>> cycles; for (int i = 0; i < int(perm.size()); i++) { int target = perm[i]; if (target == -1) continue; cycles.emplace_back(); while (true) { cycles.back().push_back(target); int next_target = perm[target]; perm[target] = -1; if (target == i) break; target = next_target; } } return cycles; } void solve() { int h, w; cin >> h >> w; vector<vector<int>> grid(h, vector<int>(w)); int which = 1; vector<char> mapping; mapping.push_back('.'); for (int row = 0; row < h; row++) { for (int col = 0; col < w; col++) { char c; cin >> c; if (c == '.') grid[row][col] = 0; else { grid[row][col] = which++; mapping.push_back(c); } } } int k; cin >> k; string s; cin >> s; // cout << s << endl; // cout << "~~~~~~" << endl; // onemove(grid, s[0]); int i = 1; while (i < k) { if (is_vertical(s[i]) != is_vertical(s[i - 1])) break; i++; } onemove(grid, s[i - 1]); i++; while (i < k) { if (is_vertical(s[i]) != is_vertical(s[i - 1])) break; i++; } if (i <= k) onemove(grid, s[i - 1]); if (i >= k) { print_grid(grid, mapping); return; } s = s.substr(i, string::npos); // // cerr << s << endl; // cerr << s << endl; s = normalize(s); // cerr << s << endl; if (s.size() >= 4 + s.size() % 4) { for (int j = 0; j < int(4 + s.size() % 4); j++) onemove(grid, s[j]); string rep = s.substr(s.size() - 4, string::npos); // cerr << rep << endl; auto v = get_perm(grid, rep); apply_perm(grid, v, (s.size() / 4) - 1); // s.resize(s.size() % 4); } else { for (char c : s) onemove(grid, c); } // cerr << s << endl; // int new_size = 120 + s.size() % 120; // if (new_size < int(s.size())) { // s.resize(new_size); // } // print_grid(grid); // onemove(grid, s[0]); // if (s.size() > 0) { // onemove(grid, s[1]); // } // if (s.size() > 4) { // onemove(grid, s[s.size() - 2]); // } // if (s.size() > 3) { // onemove(grid, s[s.size() - 1]); // } // char c; // while (cin >> c) onemove(grid, c); // cout << seen.size() << endl; print_grid(grid, mapping); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); // #ifdef LOCAL // int t; cin >> t; while (t--) // #endif solve(); cout.flush(); } |