/* Zadanie: Łamigłówka Autor: Tomasz Kwiatkowski */ #include <bits/stdc++.h> #define fi first #define se second #define pb push_back using namespace std; typedef long long ll; const int MAXN = 507; const int INF = 1e9 + 7; struct vex { int num; char c, nc; int i, j; vex() { static int i = 0; num = ++i; } }; vex org[MAXN][MAXN]; vex grid[MAXN][MAXN]; bool vis[2*MAXN*MAXN]; int n, m; void move(char mv) { if (mv == 'G') { for (int c = 0; c < m; ++c) { int i = 0; for (int r = 0; r < n; ++r) { if (grid[r][c].c != '.') swap(grid[r][c], grid[i++][c]); } } } else if (mv == 'D') { for (int c = 0; c < m; ++c) { int i = n - 1; for (int r = n - 1; r >= 0; --r) { if (grid[r][c].c != '.') swap(grid[r][c], grid[i--][c]); } } } else if (mv == 'L') { for (int r = 0; r < n; ++r) { int i = 0; for (int c = 0; c < m; ++c) { if (grid[r][c].c != '.') swap(grid[r][c], grid[r][i++]); } } } else { for (int r = 0; r < n; ++r) { int i = m - 1; for (int c = m - 1; c >= 0; --c) { if (grid[r][c].c != '.') swap(grid[r][c], grid[r][i--]); } } } } void print() { for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) cout << grid[i][j].c; cout << '\n'; } } string reduce(string& s) { bool f[100] = {}; f['L'] = f['P'] = 1; string res = ""; for (auto c : s) { if (res.size() && f[res.back()] == f[c]) res.pop_back(); if (res.size() > 1 && res[res.size() - 2] == c) continue; res.push_back(c); } return res; } void cycle(int i, int j, int k) { vex v = org[i][j]; vector<vex> c; while (!vis[v.num]) { vis[v.num] = true; c.pb(v); v = org[grid[v.i][v.j].i][grid[v.i][v.j].j]; } for (int i = 0; i < c.size(); ++i) { org[c[i].i][c[i].j].c = c[(i + k) % c.size()].c; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> grid[i][j].c; grid[i][j].i = i; grid[i][j].j = j; } } int q; cin >> q; string mvs; cin >> mvs; mvs = reduce(mvs); if (mvs.size() < 10) { for (auto mv : mvs) move(mv); print(); return 0; } move(mvs.front()); mvs.erase(mvs.begin()); move(mvs.front()); mvs.erase(mvs.begin()); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { grid[i][j].i = i; grid[i][j].j = j; org[i][j] = grid[i][j]; } } int k = mvs.size() / 4; for (int i = 0; i < 4; ++i) move(mvs[i]); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (org[i][j].c != '.') cycle(i, j, k); } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { grid[i][j] = org[i][j]; } } for (int i = 4*k; i < mvs.size(); ++i) { move(mvs[i]); } print(); 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 | /* Zadanie: Łamigłówka Autor: Tomasz Kwiatkowski */ #include <bits/stdc++.h> #define fi first #define se second #define pb push_back using namespace std; typedef long long ll; const int MAXN = 507; const int INF = 1e9 + 7; struct vex { int num; char c, nc; int i, j; vex() { static int i = 0; num = ++i; } }; vex org[MAXN][MAXN]; vex grid[MAXN][MAXN]; bool vis[2*MAXN*MAXN]; int n, m; void move(char mv) { if (mv == 'G') { for (int c = 0; c < m; ++c) { int i = 0; for (int r = 0; r < n; ++r) { if (grid[r][c].c != '.') swap(grid[r][c], grid[i++][c]); } } } else if (mv == 'D') { for (int c = 0; c < m; ++c) { int i = n - 1; for (int r = n - 1; r >= 0; --r) { if (grid[r][c].c != '.') swap(grid[r][c], grid[i--][c]); } } } else if (mv == 'L') { for (int r = 0; r < n; ++r) { int i = 0; for (int c = 0; c < m; ++c) { if (grid[r][c].c != '.') swap(grid[r][c], grid[r][i++]); } } } else { for (int r = 0; r < n; ++r) { int i = m - 1; for (int c = m - 1; c >= 0; --c) { if (grid[r][c].c != '.') swap(grid[r][c], grid[r][i--]); } } } } void print() { for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) cout << grid[i][j].c; cout << '\n'; } } string reduce(string& s) { bool f[100] = {}; f['L'] = f['P'] = 1; string res = ""; for (auto c : s) { if (res.size() && f[res.back()] == f[c]) res.pop_back(); if (res.size() > 1 && res[res.size() - 2] == c) continue; res.push_back(c); } return res; } void cycle(int i, int j, int k) { vex v = org[i][j]; vector<vex> c; while (!vis[v.num]) { vis[v.num] = true; c.pb(v); v = org[grid[v.i][v.j].i][grid[v.i][v.j].j]; } for (int i = 0; i < c.size(); ++i) { org[c[i].i][c[i].j].c = c[(i + k) % c.size()].c; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> grid[i][j].c; grid[i][j].i = i; grid[i][j].j = j; } } int q; cin >> q; string mvs; cin >> mvs; mvs = reduce(mvs); if (mvs.size() < 10) { for (auto mv : mvs) move(mv); print(); return 0; } move(mvs.front()); mvs.erase(mvs.begin()); move(mvs.front()); mvs.erase(mvs.begin()); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { grid[i][j].i = i; grid[i][j].j = j; org[i][j] = grid[i][j]; } } int k = mvs.size() / 4; for (int i = 0; i < 4; ++i) move(mvs[i]); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (org[i][j].c != '.') cycle(i, j, k); } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { grid[i][j] = org[i][j]; } } for (int i = 4*k; i < mvs.size(); ++i) { move(mvs[i]); } print(); return 0; } |