#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int N = 555; const int K = 500500; int n,m,k; char symb[N][N], moves[K]; int s[N][N], cs[N][N]; map<char,int> movenum; int st[K], stn; int q; char who[K]; int perm[K], vis[K]; void move_up() { FOR(j,m) { int pp = 0; FOR(i,n) if (s[i][j] != -1) { s[pp][j] = s[i][j]; if (pp != i) s[i][j] = -1; pp++; } } } void move_dn() { FOR(j,m) { int pp = n-1; for (int i=n-1; i >= 0; i--) if (s[i][j] != -1) { s[pp][j] = s[i][j]; if (pp != i) s[i][j] = -1; pp--; } } } void move_le() { FOR(i,n) { int pp = 0; FOR(j,m) if (s[i][j] != -1) { s[i][pp] = s[i][j]; if (pp != j) s[i][j] = -1; pp++; } } } void move_ri() { FOR(i,n) { int pp = m-1; for (int j=m-1; j >= 0; j--) if (s[i][j] != -1) { s[i][pp] = s[i][j]; if (pp != j) s[i][j] = -1; pp--; } } } void make_move(int mv) { //printf("--------------------------------------------------------\nbefore:\n"); //FOR(i,n) { // FOR(j,m) { // if (s[i][j] == -1) printf("-"); // else printf("%d", s[i][j]); // } // printf("\n"); //} if (mv == 0) move_up(); if (mv == 1) move_ri(); if (mv == 2) move_dn(); if (mv == 3) move_le(); //printf("after:\n"); //FOR(i,n) { // FOR(j,m) { // if (s[i][j] == -1) printf("-"); // else printf("%d", s[i][j]); // } // printf("\n"); //} //printf("--------------------------------------------------------\n"); } int main() { movenum['G'] = 0; movenum['P'] = 1; movenum['D'] = 2; movenum['L'] = 3; scanf("%d%d", &n, &m); FOR(i,n) scanf(" %s", symb[i]); scanf("%d %s", &k, moves); FOR(i,n) FOR(j,m) { if (symb[i][j] == '.') s[i][j] = -1; else { s[i][j] = q; who[q] = symb[i][j]; q++; } } stn = -1; FOR(i,k) { int mv = movenum[moves[i]]; while (stn >= 0 && (st[stn]+mv)%2==0) stn--; if (stn < 1 || st[stn-1] != mv) st[++stn] = mv; } //FOR(i,stn+1) make_move(st[i]); //printf("%d moves: ", stn+1); //FOR(i,stn+1) printf("%d ", st[i]); //printf("\n"); if (stn >= 0) make_move(st[0]); if (stn >= 1) make_move(st[1]); int rep = (stn+1 - 2) / 4; if (rep < 0) rep = 0; for (int i = 2; i+rep*4 <= stn; i++) make_move(st[i]); if (rep > 0) { FOR(i,n) FOR(j,m) cs[i][j] = s[i][j]; for (int i = stn-3; i <= stn; i++) make_move(st[i]); rep--; FOR(i,n) FOR(j,m) if (cs[i][j] != -1) { perm[cs[i][j]] = s[i][j]; } FOR(i,q) if (!vis[i]) { vi path; vis[i] = 1; int x = perm[i]; path.pb(i); while (x != i) { path.pb(x); vis[x] = 1; x = perm[x]; } int w = SZ(path); int shift = rep % w; FOR(j,w) perm[path[j]] = path[(j+shift)%w]; } FOR(i,n) FOR(j,m) if (s[i][j] != -1) s[i][j] = perm[s[i][j]]; } FOR(i,n) FOR(j,m) { if (s[i][j] == -1) symb[i][j] = '.'; else symb[i][j] = who[s[i][j]]; } FOR(i,n) printf("%s\n", symb[i]); return 0; }
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 | #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int N = 555; const int K = 500500; int n,m,k; char symb[N][N], moves[K]; int s[N][N], cs[N][N]; map<char,int> movenum; int st[K], stn; int q; char who[K]; int perm[K], vis[K]; void move_up() { FOR(j,m) { int pp = 0; FOR(i,n) if (s[i][j] != -1) { s[pp][j] = s[i][j]; if (pp != i) s[i][j] = -1; pp++; } } } void move_dn() { FOR(j,m) { int pp = n-1; for (int i=n-1; i >= 0; i--) if (s[i][j] != -1) { s[pp][j] = s[i][j]; if (pp != i) s[i][j] = -1; pp--; } } } void move_le() { FOR(i,n) { int pp = 0; FOR(j,m) if (s[i][j] != -1) { s[i][pp] = s[i][j]; if (pp != j) s[i][j] = -1; pp++; } } } void move_ri() { FOR(i,n) { int pp = m-1; for (int j=m-1; j >= 0; j--) if (s[i][j] != -1) { s[i][pp] = s[i][j]; if (pp != j) s[i][j] = -1; pp--; } } } void make_move(int mv) { //printf("--------------------------------------------------------\nbefore:\n"); //FOR(i,n) { // FOR(j,m) { // if (s[i][j] == -1) printf("-"); // else printf("%d", s[i][j]); // } // printf("\n"); //} if (mv == 0) move_up(); if (mv == 1) move_ri(); if (mv == 2) move_dn(); if (mv == 3) move_le(); //printf("after:\n"); //FOR(i,n) { // FOR(j,m) { // if (s[i][j] == -1) printf("-"); // else printf("%d", s[i][j]); // } // printf("\n"); //} //printf("--------------------------------------------------------\n"); } int main() { movenum['G'] = 0; movenum['P'] = 1; movenum['D'] = 2; movenum['L'] = 3; scanf("%d%d", &n, &m); FOR(i,n) scanf(" %s", symb[i]); scanf("%d %s", &k, moves); FOR(i,n) FOR(j,m) { if (symb[i][j] == '.') s[i][j] = -1; else { s[i][j] = q; who[q] = symb[i][j]; q++; } } stn = -1; FOR(i,k) { int mv = movenum[moves[i]]; while (stn >= 0 && (st[stn]+mv)%2==0) stn--; if (stn < 1 || st[stn-1] != mv) st[++stn] = mv; } //FOR(i,stn+1) make_move(st[i]); //printf("%d moves: ", stn+1); //FOR(i,stn+1) printf("%d ", st[i]); //printf("\n"); if (stn >= 0) make_move(st[0]); if (stn >= 1) make_move(st[1]); int rep = (stn+1 - 2) / 4; if (rep < 0) rep = 0; for (int i = 2; i+rep*4 <= stn; i++) make_move(st[i]); if (rep > 0) { FOR(i,n) FOR(j,m) cs[i][j] = s[i][j]; for (int i = stn-3; i <= stn; i++) make_move(st[i]); rep--; FOR(i,n) FOR(j,m) if (cs[i][j] != -1) { perm[cs[i][j]] = s[i][j]; } FOR(i,q) if (!vis[i]) { vi path; vis[i] = 1; int x = perm[i]; path.pb(i); while (x != i) { path.pb(x); vis[x] = 1; x = perm[x]; } int w = SZ(path); int shift = rep % w; FOR(j,w) perm[path[j]] = path[(j+shift)%w]; } FOR(i,n) FOR(j,m) if (s[i][j] != -1) s[i][j] = perm[s[i][j]]; } FOR(i,n) FOR(j,m) { if (s[i][j] == -1) symb[i][j] = '.'; else symb[i][j] = who[s[i][j]]; } FOR(i,n) printf("%s\n", symb[i]); return 0; } |