#include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<(int)b;++i) #define FORD(i,a,b) for(int i=a;i>=(int)b;--i) #define PB push_back #define EB emplace_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define vvll vector<vll> #define vpii vector<pii> #define pii pair<int, int> #define pll pair<ll, ll> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; using ld = long double; template <class T> istream& operator>> (istream& is, vector<T>& vec){ FOR(i,0,vec.size()) is >> vec[i]; return is; } template <class T> ostream& operator<< (ostream& os, vector<T>& vec){ for(auto& t : vec) os << t << " "; return os; } template<class T, class U> ostream& operator<< (ostream& os, const pair<T, U>& p){ os << p.FI << " " << p.SE; return os; } template<class T, class U> istream& operator>> (istream& is, pair<T, U>& p){ is >> p.FI >> p.SE; return is; } void moveb(vvi& V, int d){ if (d == 0){ FOR(c,0,V[0].size()){ int x = 0; FOR(r,0,V.size()) if (V[r][c] != -1) swap(V[r][c], V[x++][c]); } } if (d == 1){ FOR(r,0,V.size()){ int x = 0; FOR(c,0,V[0].size()) if (V[r][c] != -1) swap(V[r][c], V[r][x++]); } } if (d == 2){ FOR(c,0,V[0].size()){ int x = V.size() - 1; FORD(r,V.size()-1,0) if (V[r][c] != -1) swap(V[r][c], V[x--][c]); } } if (d == 3){ FOR(r,0,V.size()){ int x = V[0].size()-1; FORD(c,V[0].size()-1,0) if (V[r][c] != -1) swap(V[r][c], V[r][x--]); } } } vvi cycles(vi& p){ vvi res; int n = p.size(); FOR(i,0,n){ // if(p[i] >= 0){ // odkomentowac dla cykli dl. 1 if(p[i] >= 0 && p[i] != i){ res.PB({}); int t = i, tv; while(p[t] >= 0){ res.back().PB(t); tv = t; t = p[t]; p[tv] = -p[tv] - 1; } } } FOR(i,0,n) if (i != p[i]) p[i] = -p[i] - 1; return res; } vi perm_pow(vi& p, int k){ vi res(p.size()); iota(ALL(res), 0); vvi c = cycles(p); for(auto& v : c) FOR(i,0,v.size()) res[v[i]] = v[(i + k) % v.size()]; return res; } int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n, m, k; cin >> n >> m; string s; vector<string> V(n); cin >> V >> k >> s; string DIRS = "GLDP"; stack<int> S; FOR(i,0,s.size()){ int v = DIRS.find(s[i]); while(!S.empty()){ if ((S.top() ^ v) % 2 == 0) { S.pop(); continue; } if (S.size() > 1){ int w = S.top(); S.pop(); int z = S.top(); S.push(w); if (z != v) S.push(v); } else S.push(v); break; } if (S.empty()) S.push(v); } vi d; while(!S.empty()){ d.PB(S.top()); S.pop(); } reverse(ALL(d)); vvi B(n, vi(m)); string COLS = ".BC"; FOR(i,0,n) FOR(j,0,m) B[i][j] = COLS.find(V[i][j]) - 1; if (d.size() < 7){ for(int x : d) moveb(B, x); FOR(i,0,n){ FOR(j,0,m) cout << COLS[B[i][j] + 1]; cout << endl; } return 0; } moveb(B, d[0]); moveb(B, d[1]); vvi C(n, vi(m, -1)); int idx = 0; FOR(i,0,n) FOR(j,0,m) if (B[i][j] != -1) C[i][j] = idx++; //FOR(i,0,n) {FOR(j,0,m) cout << (C[i][j] == -1 ? ' ' : (char)('0' + C[i][j] % 10)); cout << endl;} moveb(C, d[2]); moveb(C, d[3]); moveb(C, d[4]); moveb(C, d[5]); //FOR(i,0,n) {FOR(j,0,m) cout << (C[i][j] == -1 ? ' ' : (char)('0' + C[i][j] % 10)); cout << endl;} vi p; FOR(i,0,n) FOR(j,0,m) if (C[i][j] != -1) p.PB(C[i][j]); //cout << p << endl; vi p2 = perm_pow(p, (d.size() - 2) / 4); //cout << p2 << endl; idx = 0; FOR(i,0,n) FOR(j,0,m) if (B[i][j] != -1) p[idx++] = B[i][j]; idx = 0; FOR(i,0,n) FOR(j,0,m) if (B[i][j] != -1) B[i][j] = p[p2[idx++]]; idx = 2 + ((d.size() - 2) / 4) * 4; while(idx < d.size()) moveb(B, d[idx++]); FOR(i,0,n){ FOR(j,0,m) cout << COLS[B[i][j] + 1]; cout << endl; } }
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 | #include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<(int)b;++i) #define FORD(i,a,b) for(int i=a;i>=(int)b;--i) #define PB push_back #define EB emplace_back #define FI first #define SE second #define umap unordered_map #define uset unordered_set #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define vvll vector<vll> #define vpii vector<pii> #define pii pair<int, int> #define pll pair<ll, ll> #define ALL(X) (X).begin(),(X).end() #ifndef DEBUG #define endl (char)10 #endif using namespace std; using ll = long long; using ld = long double; template <class T> istream& operator>> (istream& is, vector<T>& vec){ FOR(i,0,vec.size()) is >> vec[i]; return is; } template <class T> ostream& operator<< (ostream& os, vector<T>& vec){ for(auto& t : vec) os << t << " "; return os; } template<class T, class U> ostream& operator<< (ostream& os, const pair<T, U>& p){ os << p.FI << " " << p.SE; return os; } template<class T, class U> istream& operator>> (istream& is, pair<T, U>& p){ is >> p.FI >> p.SE; return is; } void moveb(vvi& V, int d){ if (d == 0){ FOR(c,0,V[0].size()){ int x = 0; FOR(r,0,V.size()) if (V[r][c] != -1) swap(V[r][c], V[x++][c]); } } if (d == 1){ FOR(r,0,V.size()){ int x = 0; FOR(c,0,V[0].size()) if (V[r][c] != -1) swap(V[r][c], V[r][x++]); } } if (d == 2){ FOR(c,0,V[0].size()){ int x = V.size() - 1; FORD(r,V.size()-1,0) if (V[r][c] != -1) swap(V[r][c], V[x--][c]); } } if (d == 3){ FOR(r,0,V.size()){ int x = V[0].size()-1; FORD(c,V[0].size()-1,0) if (V[r][c] != -1) swap(V[r][c], V[r][x--]); } } } vvi cycles(vi& p){ vvi res; int n = p.size(); FOR(i,0,n){ // if(p[i] >= 0){ // odkomentowac dla cykli dl. 1 if(p[i] >= 0 && p[i] != i){ res.PB({}); int t = i, tv; while(p[t] >= 0){ res.back().PB(t); tv = t; t = p[t]; p[tv] = -p[tv] - 1; } } } FOR(i,0,n) if (i != p[i]) p[i] = -p[i] - 1; return res; } vi perm_pow(vi& p, int k){ vi res(p.size()); iota(ALL(res), 0); vvi c = cycles(p); for(auto& v : c) FOR(i,0,v.size()) res[v[i]] = v[(i + k) % v.size()]; return res; } int main () { ios_base::sync_with_stdio(false); cin.tie(0); int n, m, k; cin >> n >> m; string s; vector<string> V(n); cin >> V >> k >> s; string DIRS = "GLDP"; stack<int> S; FOR(i,0,s.size()){ int v = DIRS.find(s[i]); while(!S.empty()){ if ((S.top() ^ v) % 2 == 0) { S.pop(); continue; } if (S.size() > 1){ int w = S.top(); S.pop(); int z = S.top(); S.push(w); if (z != v) S.push(v); } else S.push(v); break; } if (S.empty()) S.push(v); } vi d; while(!S.empty()){ d.PB(S.top()); S.pop(); } reverse(ALL(d)); vvi B(n, vi(m)); string COLS = ".BC"; FOR(i,0,n) FOR(j,0,m) B[i][j] = COLS.find(V[i][j]) - 1; if (d.size() < 7){ for(int x : d) moveb(B, x); FOR(i,0,n){ FOR(j,0,m) cout << COLS[B[i][j] + 1]; cout << endl; } return 0; } moveb(B, d[0]); moveb(B, d[1]); vvi C(n, vi(m, -1)); int idx = 0; FOR(i,0,n) FOR(j,0,m) if (B[i][j] != -1) C[i][j] = idx++; //FOR(i,0,n) {FOR(j,0,m) cout << (C[i][j] == -1 ? ' ' : (char)('0' + C[i][j] % 10)); cout << endl;} moveb(C, d[2]); moveb(C, d[3]); moveb(C, d[4]); moveb(C, d[5]); //FOR(i,0,n) {FOR(j,0,m) cout << (C[i][j] == -1 ? ' ' : (char)('0' + C[i][j] % 10)); cout << endl;} vi p; FOR(i,0,n) FOR(j,0,m) if (C[i][j] != -1) p.PB(C[i][j]); //cout << p << endl; vi p2 = perm_pow(p, (d.size() - 2) / 4); //cout << p2 << endl; idx = 0; FOR(i,0,n) FOR(j,0,m) if (B[i][j] != -1) p[idx++] = B[i][j]; idx = 0; FOR(i,0,n) FOR(j,0,m) if (B[i][j] != -1) B[i][j] = p[p2[idx++]]; idx = 2 + ((d.size() - 2) / 4) * 4; while(idx < d.size()) moveb(B, d[idx++]); FOR(i,0,n){ FOR(j,0,m) cout << COLS[B[i][j] + 1]; cout << endl; } } |