#include<bits/stdc++.h> using namespace std; #define fwd(i, a, b) for(int i=(a);i<(int)(b);i++) #define rep(i, n) for(int i=0;i<(int)(n);i++) #define all(X) (X).begin(), (X).end() #define sz(X) ((int)(X).size()) #define mp make_pair #define st first #define nd second typedef long long ll; typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<string> vs; int n,m; vs moveLR(const vs& tab, int dir) { vs res(n, string(m,'.')); rep(i,n) { int last = (dir == 1 ? 0 : m-1); for(int j=(dir == 1 ? 0 : m-1), cnt=0; cnt<m; cnt++, j+=dir) { if(tab[i][j] != '.') { res[i][last] = tab[i][j]; last+=dir; } } } return res; } vs moveUD(const vs& tab, int dir) { vs res(n, string(m,'.')); rep(j,m) { int last = (dir == 1 ? 0 : n-1); for(int i=(dir == 1 ? 0 : n-1), cnt=0; cnt<n; cnt++, i+=dir) { if(tab[i][j] != '.') { res[last][j] = tab[i][j]; last += dir; } } } return res; } typedef vector<vi> vvi; vvi moveLR(const vvi& tab, int dir) { vvi res(n, vi(m,0)); rep(i,n) { int last = (dir == 1 ? 0 : m-1); for(int j=(dir == 1 ? 0 : m-1), cnt=0; cnt<m; cnt++, j+=dir) { if(tab[i][j] != 0) { res[i][last] = tab[i][j]; last+=dir; } } } return res; } vvi moveUD(const vvi& tab, int dir) { vvi res(n, vi(m,0)); rep(j,m) { int last = (dir == 1 ? 0 : n-1); for(int i=(dir == 1 ? 0 : n-1), cnt=0; cnt<n; cnt++, i+=dir) { if(tab[i][j] != 0) { res[last][j] = tab[i][j]; last += dir; } } } return res; } bool isVer(char c) { return c == 'G' || c == 'D'; } template<class T> T mov(const T& tab, char dir) { if(isVer(dir)) if(dir == 'G') return moveUD(tab,1); else return moveUD(tab,-1); else { if(dir == 'L') return moveLR(tab,1); else return moveLR(tab,-1); } } char inv(char c) { map<char,char> M = {{'G', 'D'}, {'D', 'G'}, {'L', 'P'}, {'P', 'L'}}; return M[c]; } vi compose(vi a, vi b) { vi res(sz(a)); rep(i,sz(a)) res[i] = a[b[i]]; return res; } vi fme(vi a, ll b) { if(b == 0) { vi res(sz(a)); iota(all(res),0); return res; } if(b&1) return compose(fme(a,b-1),a); vi tmp = fme(a,b/2); return compose(tmp,tmp); } vs process(vs tab, string s) { int i=0; while(i < sz(s) && isVer(s[i]) == isVer(s[0])) i++; tab = mov(tab, s[i-1]); if(i != sz(s)) tab = mov(tab,s[i++]); string toApply; fwd(j,i,sz(s)) { toApply.push_back(s[j]); while(sz(toApply) >= 2) { if(toApply.back() == toApply[sz(toApply)-2]) toApply.pop_back(); else if(toApply.back() == inv(toApply[sz(toApply)-2])) {int v = toApply.back(); toApply.pop_back(); toApply.back() = v;} else if(sz(toApply) >= 3 && toApply.back() == toApply[sz(toApply)-3]) toApply.pop_back(); else break; } } reverse(all(toApply)); int iters = 0; while(sz(toApply) > 0 && (iters < 4 || sz(toApply)%4 != 0)) { tab = mov(tab,toApply.back()); toApply.pop_back(); iters++; } reverse(all(toApply)); if(sz(toApply) == 0) return tab; vvi org(n, vi(m)); string orgColor = "."; int cnt=1; rep(i,n) rep(j,m) if(tab[i][j] != '.') { org[i][j] = cnt++; orgColor.push_back(tab[i][j]); } vvi after = mov(mov(mov(mov(org,toApply[0]), toApply[1]), toApply[2]), toApply[3]); vi perm(cnt); rep(i,n) rep(j,m) perm[org[i][j]] = after[i][j]; perm = fme(perm, sz(toApply)/4); vs res(n, string(m,'.')); rep(i,n) rep(j,m) res[i][j] = orgColor[perm[org[i][j]]]; return res; } int32_t main(){ ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; vs tab(n); rep(i,n) cin >> tab[i]; int tmp; cin >> tmp; string s; cin >> s; for(auto a:process(tab, s)) cout<<a<<"\n"; }
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> using namespace std; #define fwd(i, a, b) for(int i=(a);i<(int)(b);i++) #define rep(i, n) for(int i=0;i<(int)(n);i++) #define all(X) (X).begin(), (X).end() #define sz(X) ((int)(X).size()) #define mp make_pair #define st first #define nd second typedef long long ll; typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<string> vs; int n,m; vs moveLR(const vs& tab, int dir) { vs res(n, string(m,'.')); rep(i,n) { int last = (dir == 1 ? 0 : m-1); for(int j=(dir == 1 ? 0 : m-1), cnt=0; cnt<m; cnt++, j+=dir) { if(tab[i][j] != '.') { res[i][last] = tab[i][j]; last+=dir; } } } return res; } vs moveUD(const vs& tab, int dir) { vs res(n, string(m,'.')); rep(j,m) { int last = (dir == 1 ? 0 : n-1); for(int i=(dir == 1 ? 0 : n-1), cnt=0; cnt<n; cnt++, i+=dir) { if(tab[i][j] != '.') { res[last][j] = tab[i][j]; last += dir; } } } return res; } typedef vector<vi> vvi; vvi moveLR(const vvi& tab, int dir) { vvi res(n, vi(m,0)); rep(i,n) { int last = (dir == 1 ? 0 : m-1); for(int j=(dir == 1 ? 0 : m-1), cnt=0; cnt<m; cnt++, j+=dir) { if(tab[i][j] != 0) { res[i][last] = tab[i][j]; last+=dir; } } } return res; } vvi moveUD(const vvi& tab, int dir) { vvi res(n, vi(m,0)); rep(j,m) { int last = (dir == 1 ? 0 : n-1); for(int i=(dir == 1 ? 0 : n-1), cnt=0; cnt<n; cnt++, i+=dir) { if(tab[i][j] != 0) { res[last][j] = tab[i][j]; last += dir; } } } return res; } bool isVer(char c) { return c == 'G' || c == 'D'; } template<class T> T mov(const T& tab, char dir) { if(isVer(dir)) if(dir == 'G') return moveUD(tab,1); else return moveUD(tab,-1); else { if(dir == 'L') return moveLR(tab,1); else return moveLR(tab,-1); } } char inv(char c) { map<char,char> M = {{'G', 'D'}, {'D', 'G'}, {'L', 'P'}, {'P', 'L'}}; return M[c]; } vi compose(vi a, vi b) { vi res(sz(a)); rep(i,sz(a)) res[i] = a[b[i]]; return res; } vi fme(vi a, ll b) { if(b == 0) { vi res(sz(a)); iota(all(res),0); return res; } if(b&1) return compose(fme(a,b-1),a); vi tmp = fme(a,b/2); return compose(tmp,tmp); } vs process(vs tab, string s) { int i=0; while(i < sz(s) && isVer(s[i]) == isVer(s[0])) i++; tab = mov(tab, s[i-1]); if(i != sz(s)) tab = mov(tab,s[i++]); string toApply; fwd(j,i,sz(s)) { toApply.push_back(s[j]); while(sz(toApply) >= 2) { if(toApply.back() == toApply[sz(toApply)-2]) toApply.pop_back(); else if(toApply.back() == inv(toApply[sz(toApply)-2])) {int v = toApply.back(); toApply.pop_back(); toApply.back() = v;} else if(sz(toApply) >= 3 && toApply.back() == toApply[sz(toApply)-3]) toApply.pop_back(); else break; } } reverse(all(toApply)); int iters = 0; while(sz(toApply) > 0 && (iters < 4 || sz(toApply)%4 != 0)) { tab = mov(tab,toApply.back()); toApply.pop_back(); iters++; } reverse(all(toApply)); if(sz(toApply) == 0) return tab; vvi org(n, vi(m)); string orgColor = "."; int cnt=1; rep(i,n) rep(j,m) if(tab[i][j] != '.') { org[i][j] = cnt++; orgColor.push_back(tab[i][j]); } vvi after = mov(mov(mov(mov(org,toApply[0]), toApply[1]), toApply[2]), toApply[3]); vi perm(cnt); rep(i,n) rep(j,m) perm[org[i][j]] = after[i][j]; perm = fme(perm, sz(toApply)/4); vs res(n, string(m,'.')); rep(i,n) rep(j,m) res[i][j] = orgColor[perm[org[i][j]]]; return res; } int32_t main(){ ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; vs tab(n); rep(i,n) cin >> tab[i]; int tmp; cin >> tmp; string s; cin >> s; for(auto a:process(tab, s)) cout<<a<<"\n"; } |