#include<bits/stdc++.h> #define ll long long using namespace std; const int N=505; char rev(char x) { if (x=='G') { return 'D'; } if (x=='D') { return 'G'; } if (x=='L') { return 'P'; } if (x=='P') { return 'L'; } return 0; } bool vis[N*N]; int n,m,k,a[N][N],b[N][N],p[N*N],x[N*N],y[N*N]; vector<char>opt; void perform(char opt,int (*a)[N]) { if (opt=='L') { for (int i=1;i<=n;i++) { for (int j=1,k=1;j<=m;j++) { if (a[i][j]) { a[i][k++]=a[i][j]; if (k<=j) { a[i][j]=0; } } } } } if (opt=='P') { for (int i=1;i<=n;i++) { for (int j=m,k=m;j>=1;j--) { if (a[i][j]) { a[i][k--]=a[i][j]; if (k>=j) { a[i][j]=0; } } } } } if (opt=='G') { for (int j=1;j<=m;j++) { for (int i=1,k=1;i<=n;i++) { if (a[i][j]) { a[k++][j]=a[i][j]; if (k<=i) { a[i][j]=0; } } } } } if (opt=='D') { for (int j=1;j<=m;j++) { for (int i=n,k=n;i>=1;i--) { if (a[i][j]) { a[k--][j]=a[i][j]; if (k>=i) { a[i][j]=0; } } } } } } signed main() { ios::sync_with_stdio(false),cin.tie(0); cout.precision(10),cout.setf(ios::fixed); cin>>n>>m; for (int i=1;i<=n;i++) { string s; cin>>s; for (int j=1;j<=m;j++) { a[i][j]=s[j-1]=='.'?0:(s[j-1]=='B'?1:2); } } string s; cin>>k>>s; for (char i:s) { if (opt.empty()) { opt.emplace_back(i); } else if (opt.back()!=i) { if (i==rev(opt.back())) { opt.pop_back(); } if (opt.size()>=1&&i==opt[(int)opt.size()-1]) { continue; } if (opt.size()>=2&&i==opt[(int)opt.size()-2]) { continue; } opt.emplace_back(i); } }/* for (char i:opt) { cerr<<i; } cerr<<endl; for (int i=1;i<=n;i++) { for (int j=1;j<=m;j++) { b[i][j]=a[i][j]; } } for (char i:opt) { perform(i,b); } for (int i=1;i<=n;i++) { for (int j=1;j<=m;j++) { cout<<".BC"[b[i][j]]; b[i][j]=0; } cout<<"\n"; }*/ if (opt.size()<6) { for (char i:opt) { perform(i,a); } } else { for (int i=0;i<2;i++) { perform(opt[i],a); } int cnt=0; for (int i=1;i<=n;i++) { for (int j=1;j<=m;j++) { if (a[i][j]) { b[i][j]=++cnt; x[cnt]=i,y[cnt]=j; } } }/* for (int i=1;i<=n;i++) { for (int j=1;j<=m;j++) { cout<<".BC"[a[i][j]]; } cout<<"\n"; }*/ for (int i=2;i<6;i++) { perform(opt[i],b); }/* for (int i=1;i<=n;i++) { for (int j=1;j<=m;j++) { cout<<b[i][j]<<' '; } cout<<"\n"; }*/ cnt=0; for (int i=1;i<=n;i++) { for (int j=1;j<=m;j++) { if (a[i][j]) { p[b[i][j]]=++cnt; } } } int now=2,t=0; while (now+4<=(int)opt.size()) { now+=4; t++; } for (int i=1;i<=cnt;i++) { if (vis[i]) { continue; } vector<int>b,c; for (int j=i;!vis[j];j=p[j]) { vis[j]=1; b.emplace_back(j); c.emplace_back(a[x[p[j]]][y[p[j]]]); } for (int j=0;j<(int)b.size();j++) { int id=p[b[(j+t)%b.size()]]; a[x[id]][y[id]]=c[j]; } } while (now<(int)opt.size()) { perform(opt[now++],a); } } for (int i=1;i<=n;i++) { for (int j=1;j<=m;j++) { cout<<".BC"[a[i][j]]; } cout<<"\n"; } return 0; } /* 4 4 BBB. C.CC B.B. CCBB 20 GPDDPPPGDPGLGDPPPLLP */
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 | #include<bits/stdc++.h> #define ll long long using namespace std; const int N=505; char rev(char x) { if (x=='G') { return 'D'; } if (x=='D') { return 'G'; } if (x=='L') { return 'P'; } if (x=='P') { return 'L'; } return 0; } bool vis[N*N]; int n,m,k,a[N][N],b[N][N],p[N*N],x[N*N],y[N*N]; vector<char>opt; void perform(char opt,int (*a)[N]) { if (opt=='L') { for (int i=1;i<=n;i++) { for (int j=1,k=1;j<=m;j++) { if (a[i][j]) { a[i][k++]=a[i][j]; if (k<=j) { a[i][j]=0; } } } } } if (opt=='P') { for (int i=1;i<=n;i++) { for (int j=m,k=m;j>=1;j--) { if (a[i][j]) { a[i][k--]=a[i][j]; if (k>=j) { a[i][j]=0; } } } } } if (opt=='G') { for (int j=1;j<=m;j++) { for (int i=1,k=1;i<=n;i++) { if (a[i][j]) { a[k++][j]=a[i][j]; if (k<=i) { a[i][j]=0; } } } } } if (opt=='D') { for (int j=1;j<=m;j++) { for (int i=n,k=n;i>=1;i--) { if (a[i][j]) { a[k--][j]=a[i][j]; if (k>=i) { a[i][j]=0; } } } } } } signed main() { ios::sync_with_stdio(false),cin.tie(0); cout.precision(10),cout.setf(ios::fixed); cin>>n>>m; for (int i=1;i<=n;i++) { string s; cin>>s; for (int j=1;j<=m;j++) { a[i][j]=s[j-1]=='.'?0:(s[j-1]=='B'?1:2); } } string s; cin>>k>>s; for (char i:s) { if (opt.empty()) { opt.emplace_back(i); } else if (opt.back()!=i) { if (i==rev(opt.back())) { opt.pop_back(); } if (opt.size()>=1&&i==opt[(int)opt.size()-1]) { continue; } if (opt.size()>=2&&i==opt[(int)opt.size()-2]) { continue; } opt.emplace_back(i); } }/* for (char i:opt) { cerr<<i; } cerr<<endl; for (int i=1;i<=n;i++) { for (int j=1;j<=m;j++) { b[i][j]=a[i][j]; } } for (char i:opt) { perform(i,b); } for (int i=1;i<=n;i++) { for (int j=1;j<=m;j++) { cout<<".BC"[b[i][j]]; b[i][j]=0; } cout<<"\n"; }*/ if (opt.size()<6) { for (char i:opt) { perform(i,a); } } else { for (int i=0;i<2;i++) { perform(opt[i],a); } int cnt=0; for (int i=1;i<=n;i++) { for (int j=1;j<=m;j++) { if (a[i][j]) { b[i][j]=++cnt; x[cnt]=i,y[cnt]=j; } } }/* for (int i=1;i<=n;i++) { for (int j=1;j<=m;j++) { cout<<".BC"[a[i][j]]; } cout<<"\n"; }*/ for (int i=2;i<6;i++) { perform(opt[i],b); }/* for (int i=1;i<=n;i++) { for (int j=1;j<=m;j++) { cout<<b[i][j]<<' '; } cout<<"\n"; }*/ cnt=0; for (int i=1;i<=n;i++) { for (int j=1;j<=m;j++) { if (a[i][j]) { p[b[i][j]]=++cnt; } } } int now=2,t=0; while (now+4<=(int)opt.size()) { now+=4; t++; } for (int i=1;i<=cnt;i++) { if (vis[i]) { continue; } vector<int>b,c; for (int j=i;!vis[j];j=p[j]) { vis[j]=1; b.emplace_back(j); c.emplace_back(a[x[p[j]]][y[p[j]]]); } for (int j=0;j<(int)b.size();j++) { int id=p[b[(j+t)%b.size()]]; a[x[id]][y[id]]=c[j]; } } while (now<(int)opt.size()) { perform(opt[now++],a); } } for (int i=1;i<=n;i++) { for (int j=1;j<=m;j++) { cout<<".BC"[a[i][j]]; } cout<<"\n"; } return 0; } /* 4 4 BBB. C.CC B.B. CCBB 20 GPDDPPPGDPGLGDPPPLLP */ |