#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define eb emplace_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef basic_string<int> BI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937 mrand(1); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=510; int n,m,k2; char s[N][N]; char op2[501000],op[501000]; array<int,3> pos[N*N]; int go[N*N],ret[N*N],vis[N*N]; int pid[N][N],qid[N][N],id[N][N]; int main() { scanf("%d%d",&n,&m); rep(i,0,n) scanf("%s",s[i]); scanf("%d",&k2); scanf("%s",op2); int k=0; rep(i,0,k2) { op[k++]=op2[i]; auto reduce=[&]() { if (k>=2) { int p1=op[k-2]=='L'||op[k-2]=='P'; int p2=op[k-1]=='L'||op[k-1]=='P'; if (p1==p2) { op[k-2]=op[k-1],--k; return true; } } if (k>=3&&op[k-3]==op[k-1]) { --k; return true; } return false; }; while (reduce()); } op[k]=0; //puts(op); int tot=0; rep(i,0,n) rep(j,0,m) if (s[i][j]!='.') { pos[tot++]={i,j,s[i][j]-'A'}; } auto gao=[&](char u) { rep(i,0,n) rep(j,0,m) id[i][j]=-1; rep(i,0,tot) id[pos[i][0]][pos[i][1]]=i; /*rep(i,0,n) { rep(j,0,m) { if (id[i][j]!=-1) printf("%c",'A'+pos[id[i][j]][2]); else printf("."); } puts(""); } puts("");*/ if (u=='L'||u=='P') { rep(i,0,n) { VI vr; rep(j,0,m) if (id[i][j]!=-1) vr.pb(id[i][j]); if (u=='L') { rep(j,0,SZ(vr)) pos[vr[j]][1]=j; } else { rep(j,0,SZ(vr)) pos[vr[j]][1]=m-SZ(vr)+j; } } } else { rep(j,0,m) { VI vc; rep(i,0,n) if (id[i][j]!=-1) vc.pb(id[i][j]); if (u=='G') { rep(i,0,SZ(vc)) pos[vc[i]][0]=i; } else { rep(i,0,SZ(vc)) pos[vc[i]][0]=n-SZ(vc)+i; } } } }; if (k<=15) { rep(i,0,k) gao(op[i]); } else { int pc=0; rep(i,0,4) gao(op[i]); rep(i,4,10) if (i%4==k%4) { pc=i; break; } else { gao(op[i]); } rep(i,0,n) rep(j,0,m) pid[i][j]=-1; rep(i,0,tot) pid[pos[i][0]][pos[i][1]]=i; rep(i,0,4) gao(op[pc+i]); int rd=(k-pc)/4; //pc+=4; rep(i,0,n) rep(j,0,m) qid[i][j]=-1; rep(i,0,tot) qid[pos[i][0]][pos[i][1]]=i; rep(i,0,n) rep(j,0,m) if (pid[i][j]!=-1) { assert(qid[i][j]!=-1); go[pid[i][j]]=qid[i][j]; } rep(i,0,tot) if (!vis[i]) { VI cyc; int u=i; vis[i]=1; while (1) { cyc.pb(u); u=go[u]; if (vis[u]) break; vis[u]=1; } rep(j,0,SZ(cyc)) ret[cyc[j]]=cyc[(j+rd)%SZ(cyc)]; } rep(i,0,n) rep(j,0,m) if (pid[i][j]!=-1) { pos[ret[pid[i][j]]][0]=i; pos[ret[pid[i][j]]][1]=j; } } //rep(i,0,k) gao(op[i]); rep(i,0,n) rep(j,0,m) s[i][j]='.'; rep(i,0,tot) s[pos[i][0]][pos[i][1]]='A'+pos[i][2]; rep(i,0,n) puts(s[i]); }
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 | #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define eb emplace_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef basic_string<int> BI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937 mrand(1); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=510; int n,m,k2; char s[N][N]; char op2[501000],op[501000]; array<int,3> pos[N*N]; int go[N*N],ret[N*N],vis[N*N]; int pid[N][N],qid[N][N],id[N][N]; int main() { scanf("%d%d",&n,&m); rep(i,0,n) scanf("%s",s[i]); scanf("%d",&k2); scanf("%s",op2); int k=0; rep(i,0,k2) { op[k++]=op2[i]; auto reduce=[&]() { if (k>=2) { int p1=op[k-2]=='L'||op[k-2]=='P'; int p2=op[k-1]=='L'||op[k-1]=='P'; if (p1==p2) { op[k-2]=op[k-1],--k; return true; } } if (k>=3&&op[k-3]==op[k-1]) { --k; return true; } return false; }; while (reduce()); } op[k]=0; //puts(op); int tot=0; rep(i,0,n) rep(j,0,m) if (s[i][j]!='.') { pos[tot++]={i,j,s[i][j]-'A'}; } auto gao=[&](char u) { rep(i,0,n) rep(j,0,m) id[i][j]=-1; rep(i,0,tot) id[pos[i][0]][pos[i][1]]=i; /*rep(i,0,n) { rep(j,0,m) { if (id[i][j]!=-1) printf("%c",'A'+pos[id[i][j]][2]); else printf("."); } puts(""); } puts("");*/ if (u=='L'||u=='P') { rep(i,0,n) { VI vr; rep(j,0,m) if (id[i][j]!=-1) vr.pb(id[i][j]); if (u=='L') { rep(j,0,SZ(vr)) pos[vr[j]][1]=j; } else { rep(j,0,SZ(vr)) pos[vr[j]][1]=m-SZ(vr)+j; } } } else { rep(j,0,m) { VI vc; rep(i,0,n) if (id[i][j]!=-1) vc.pb(id[i][j]); if (u=='G') { rep(i,0,SZ(vc)) pos[vc[i]][0]=i; } else { rep(i,0,SZ(vc)) pos[vc[i]][0]=n-SZ(vc)+i; } } } }; if (k<=15) { rep(i,0,k) gao(op[i]); } else { int pc=0; rep(i,0,4) gao(op[i]); rep(i,4,10) if (i%4==k%4) { pc=i; break; } else { gao(op[i]); } rep(i,0,n) rep(j,0,m) pid[i][j]=-1; rep(i,0,tot) pid[pos[i][0]][pos[i][1]]=i; rep(i,0,4) gao(op[pc+i]); int rd=(k-pc)/4; //pc+=4; rep(i,0,n) rep(j,0,m) qid[i][j]=-1; rep(i,0,tot) qid[pos[i][0]][pos[i][1]]=i; rep(i,0,n) rep(j,0,m) if (pid[i][j]!=-1) { assert(qid[i][j]!=-1); go[pid[i][j]]=qid[i][j]; } rep(i,0,tot) if (!vis[i]) { VI cyc; int u=i; vis[i]=1; while (1) { cyc.pb(u); u=go[u]; if (vis[u]) break; vis[u]=1; } rep(j,0,SZ(cyc)) ret[cyc[j]]=cyc[(j+rd)%SZ(cyc)]; } rep(i,0,n) rep(j,0,m) if (pid[i][j]!=-1) { pos[ret[pid[i][j]]][0]=i; pos[ret[pid[i][j]]][1]=j; } } //rep(i,0,k) gao(op[i]); rep(i,0,n) rep(j,0,m) s[i][j]='.'; rep(i,0,tot) s[pos[i][0]][pos[i][1]]='A'+pos[i][2]; rep(i,0,n) puts(s[i]); } |