#include <bits/stdc++.h> using namespace std; void change(vector<vector<int>>&t, char c) { int n=t.size(), m=t[0].size(); if (c=='G') { for (int j=0; j<m; j++) for (int i=0, s=0; i<n; i++) if (t[i][j]) swap(t[i][j], t[s++][j]); } else if (c=='D') { for (int j=0; j<m; j++) for (int i=n-1, s=n-1; i>=0; i--) if (t[i][j]) swap(t[i][j], t[s--][j]); } else if (c=='L') { for (int i=0; i<n; i++) for (int j=0, s=0; j<m; j++) if (t[i][j]) swap(t[i][j], t[i][s++]); } else if (c=='P') { for (int i=0; i<n; i++) for (int j=m-1, s=m-1; j>=0; j--) if (t[i][j]) swap(t[i][j], t[i][s--]); } else assert("change: zly argument c" || false); } void print(vector<vector<int>>&t) { for (auto i : t) { for (auto j : i) cout<<(j==0? '.' : j==1? 'B' : 'C'); cout<<"\n"; } //~ cout<<endl; } void print2(vector<vector<int>>&t) { for (auto i : t) { for (auto j : i) cout<<j; cout<<"\n"; } cout<<endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin>>n>>m; vector t(n, vector(m, 0)); for (auto &i : t) for (auto &j : i) { char c; cin>>c; if (c=='B') j=1; else if (c=='C') j=2; } int k; cin>>k; string c="#"; vector<char>odw(200), dir[2]={{0}, {0}}; odw['G']='D'; odw['D']='G'; odw['L']='P'; odw['P']='L'; for (int i=0; i<k; i++) { char z; cin>>z; if (c.back()==odw[z]) c.pop_back(), dir[0].pop_back(), dir[1].pop_back(); bool pom=(z=='G' || z=='D')? 0 : 1; if (dir[pom].back()!=z) c.push_back(z), dir[pom].push_back(z), dir[!pom].push_back(dir[!pom].back()); } if (c.length()<=10) { for (int i=1; i<(int)c.length(); i++) change(t, c[i]); print(t); return 0; } for (int i=1; i<=2; i++) change(t, c[i]); //~ cout<<"\n"; //~ print(t); vector shape(n, vector(m, 0)); for (int i=0; i<n; i++) for (int j=0; j<m; j++) if (t[i][j]) shape[i][j]=i*m+j+1; //~ print2(shape); for (int i=3; i<=6; i++) change(shape, c[i]); //~ print2(shape); vector jmp(n, vector(m, vector(17, 0))); for (int i=0; i<n; i++) for (int j=0; j<m; j++) if (shape[i][j]) { int x=(shape[i][j]-1)/m, y=(shape[i][j]-1)%m; jmp[x][y][0]=i*m+j+1; } for (int l=1; l<17; l++) for (int i=0; i<n; i++) for (int j=0; j<m; j++) if (shape[i][j]) { int x=(jmp[i][j][l-1]-1)/m, y=(jmp[i][j][l-1]-1)%m; jmp[i][j][l]=jmp[x][y][l-1]; } //~ for (int l=0; l<2; l++) //~ for (int i=0; i<n; i++) //~ for (int j=0; j<m; j++) //~ if (jmp[i][j][0]) //~ { //~ int x=(jmp[i][j][0]-1)/m, y=(jmp[i][j][0]-1)%m; //~ cout<<l<<": "<<i<<" "<<j<<" "<<x<<" "<<y<<"\n"; //~ } for (int i=3; i<(int)c.length();) { int l=-1; while (i+4*(1<<(l+1))<=(int)c.length()) l++; if (l==-1) while (i<(int)c.length()) change(t, c[i]), i++; else { vector swp(n, vector(m, 0)); for (int ii=0; ii<n; ii++) for (int jj=0; jj<m; jj++) if (shape[ii][jj]) { int x=(jmp[ii][jj][l]-1)/m, y=(jmp[ii][jj][l]-1)%m; swp[x][y]=t[ii][jj]; } swap(swp, t); i+=4*(1<<l); } } print(t); }
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 | #include <bits/stdc++.h> using namespace std; void change(vector<vector<int>>&t, char c) { int n=t.size(), m=t[0].size(); if (c=='G') { for (int j=0; j<m; j++) for (int i=0, s=0; i<n; i++) if (t[i][j]) swap(t[i][j], t[s++][j]); } else if (c=='D') { for (int j=0; j<m; j++) for (int i=n-1, s=n-1; i>=0; i--) if (t[i][j]) swap(t[i][j], t[s--][j]); } else if (c=='L') { for (int i=0; i<n; i++) for (int j=0, s=0; j<m; j++) if (t[i][j]) swap(t[i][j], t[i][s++]); } else if (c=='P') { for (int i=0; i<n; i++) for (int j=m-1, s=m-1; j>=0; j--) if (t[i][j]) swap(t[i][j], t[i][s--]); } else assert("change: zly argument c" || false); } void print(vector<vector<int>>&t) { for (auto i : t) { for (auto j : i) cout<<(j==0? '.' : j==1? 'B' : 'C'); cout<<"\n"; } //~ cout<<endl; } void print2(vector<vector<int>>&t) { for (auto i : t) { for (auto j : i) cout<<j; cout<<"\n"; } cout<<endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin>>n>>m; vector t(n, vector(m, 0)); for (auto &i : t) for (auto &j : i) { char c; cin>>c; if (c=='B') j=1; else if (c=='C') j=2; } int k; cin>>k; string c="#"; vector<char>odw(200), dir[2]={{0}, {0}}; odw['G']='D'; odw['D']='G'; odw['L']='P'; odw['P']='L'; for (int i=0; i<k; i++) { char z; cin>>z; if (c.back()==odw[z]) c.pop_back(), dir[0].pop_back(), dir[1].pop_back(); bool pom=(z=='G' || z=='D')? 0 : 1; if (dir[pom].back()!=z) c.push_back(z), dir[pom].push_back(z), dir[!pom].push_back(dir[!pom].back()); } if (c.length()<=10) { for (int i=1; i<(int)c.length(); i++) change(t, c[i]); print(t); return 0; } for (int i=1; i<=2; i++) change(t, c[i]); //~ cout<<"\n"; //~ print(t); vector shape(n, vector(m, 0)); for (int i=0; i<n; i++) for (int j=0; j<m; j++) if (t[i][j]) shape[i][j]=i*m+j+1; //~ print2(shape); for (int i=3; i<=6; i++) change(shape, c[i]); //~ print2(shape); vector jmp(n, vector(m, vector(17, 0))); for (int i=0; i<n; i++) for (int j=0; j<m; j++) if (shape[i][j]) { int x=(shape[i][j]-1)/m, y=(shape[i][j]-1)%m; jmp[x][y][0]=i*m+j+1; } for (int l=1; l<17; l++) for (int i=0; i<n; i++) for (int j=0; j<m; j++) if (shape[i][j]) { int x=(jmp[i][j][l-1]-1)/m, y=(jmp[i][j][l-1]-1)%m; jmp[i][j][l]=jmp[x][y][l-1]; } //~ for (int l=0; l<2; l++) //~ for (int i=0; i<n; i++) //~ for (int j=0; j<m; j++) //~ if (jmp[i][j][0]) //~ { //~ int x=(jmp[i][j][0]-1)/m, y=(jmp[i][j][0]-1)%m; //~ cout<<l<<": "<<i<<" "<<j<<" "<<x<<" "<<y<<"\n"; //~ } for (int i=3; i<(int)c.length();) { int l=-1; while (i+4*(1<<(l+1))<=(int)c.length()) l++; if (l==-1) while (i<(int)c.length()) change(t, c[i]), i++; else { vector swp(n, vector(m, 0)); for (int ii=0; ii<n; ii++) for (int jj=0; jj<m; jj++) if (shape[ii][jj]) { int x=(jmp[ii][jj][l]-1)/m, y=(jmp[ii][jj][l]-1)%m; swp[x][y]=t[ii][jj]; } swap(swp, t); i+=4*(1<<l); } } print(t); } |