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;
typedef vector<int> VI;
typedef pair <int,int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;
const int MOD = 1000000007;
const int N = 505;
const int DEBUG = 0;

char forMoves[4] = {'P', 'D', 'L', 'G'};
char backMoves[4] = {'P', 'G', 'L', 'D'};

int n, m, i, j, k, x, tab[N][N], bat[N][N], ind, w, fr, moves, newVal[N * N], rest, amoves, fullOb, vis[N * N], actX, actY, wsk[N * N], movesBef;
char t[N], firstMove;
char t2[500005];
VI vCol, v;
char* q;

void makeMove(char c) {
	if (DEBUG) printf("%c\n", c);
	int i, j, w;
	if (c == 'G') {
		for (j=0;j<m;j++) {
			i = 0;
			while (i < n && tab[i][j]) i++;
			w = i;
			for (;i<n;i++) {
				if (tab[i][j]) tab[w++][j] = tab[i][j];
				tab[i][j] = 0;
			}
		}
	} else if (c == 'D') {
		for (j=0;j<m;j++) {
			i = n - 1;
			while (i >= 0 && tab[i][j]) i--;
			w = i;
			for (;i>=0;i--) {
				if (tab[i][j]) tab[w--][j] = tab[i][j];
				tab[i][j] = 0;
			}
		}
	} else if (c == 'L') {
		for (i=0;i<n;i++) {
			j = 0;
			while (j < m && tab[i][j]) j++;
			w = j;
			for (;j<m;j++) {
				if (tab[i][j]) tab[i][w++] = tab[i][j];
				tab[i][j] = 0;
			}
		}
	} else if (c == 'P') {
		for (i=0;i<n;i++) {
			j = m - 1;
			while (j >= 0 && tab[i][j]) j--;
			w = j;
			for (;j>=0;j--) {
				if (tab[i][j]) tab[i][w--] = tab[i][j];
				tab[i][j] = 0;
			}
		}
	}
}

void print() {
	for (int i=0;i<n;i++) {
		for (int j=0;j<m;j++) if (!tab[i][j]) printf("."); else if (vCol[tab[i][j] - 1]) printf("B"); else printf("C");
		printf("\n");
	}
}

int main() {
scanf("%d %d", &n, &m);
vCol.clear();
fr = 0;
for (i=0;i<n;i++) {
	scanf("%s", t);
	for (j=0;j<m;j++) if (t[j] == '.') tab[i][j] = 0; else {
		tab[i][j] = ++fr;
		vCol.pb(t[j] == 'B');
	}
}
scanf("%d", &x);
scanf("%s", &t2);
if (t2[0] == 'L' || t2[0] == 'P') {
	i = 0;
	while (i + 1 < x && (t2[i + 1] == 'L' || t2[i + 1] == 'P')) i++;
	makeMove(t2[i]);
	if (t2[i] == 'L') actX = -1; else actX = 1;
	i++;
	while (i + 1 < x && (t2[i + 1] == 'G' || t2[i + 1] == 'D')) i++;
	if (i < x) {
		makeMove(t2[i]);
		if (t2[i] == 'D') actY = -1; else actY = 1;
	}
	i++;
} else {
	i = 0;
	while (i + 1 < x && (t2[i + 1] == 'G' || t2[i + 1] == 'D')) i++;
	makeMove(t2[i]);
	if (t2[i] == 'D') actY = -1; else actY = 1;
	i++;
	while (i + 1 < x && (t2[i + 1] == 'L' || t2[i + 1] == 'P')) i++;
	if (i < x) {
		makeMove(t2[i]);
		if (t2[i] == 'L') actX = -1; else actX = 1;
	}
	i++;
}
moves = 0;
if (DEBUG) printf("%d %d\n", actX, actY);
for (;i<x;i++) {
	movesBef = moves;
	if (t2[i] == 'L') {
		if (actX == -1) continue;
		if (actY == -1) moves++; else moves--;
		actX = -1;
	} else if (t2[i] == 'P') {
		if (actX == 1) continue;
		if (actY == 1) moves++; else moves--;
		actX = 1;
	} else if (t2[i] == 'G') {
		if (actY == 1) continue;
		if (actX == -1) moves++; else moves--;
		actY = 1;
	} else if (t2[i] == 'D') {
		if (actY == -1) continue;
		if (actX == 1) moves++; else moves--;
		actY = -1;
	}
	if (movesBef == 0) firstMove = t2[i];
	if (DEBUG) printf("%d moves %d, firstMove %c\n", i, moves, firstMove);
}
for (i=0;i<n;i++) for (j=0;j<m;j++) bat[i][j] = tab[i][j];
if (moves == 0) {
	print();
	return 0;
}
if (moves > 0) q = forMoves; else q = backMoves;
for (i=0;i<4;i++) if (q[i] == firstMove) ind = i;
if (DEBUG) printf("moves %d, ind %d, firstMove %c\n", moves, ind, firstMove);
i = ind;
while (true) {
	makeMove(q[i]);
	i = (i + 1) % 4;
	if (i == ind) break;
}
for (i=0;i<n;i++) for (j=0;j<m;j++) if (bat[i][j] != 0) wsk[bat[i][j]] = tab[i][j];
//for (i=1;i<=fr;i++) printf("%d %d\n", i, wsk[i]);
for (i=0;i<n;i++) for (j=0;j<m;j++) tab[i][j] = bat[i][j];
amoves = abs(moves);
fullOb = amoves / 4;
for (i=1;i<=fr;i++) vis[i] = 0;
for (i=1;i<=fr;i++) if (!vis[i]) {
	v.clear();
	j = i;
	while (true) {
		v.pb(j);
		vis[j] = 1;
		if (wsk[j] == i) break;
		j = wsk[j];
	}
	for (j=0;j<v.size();j++) newVal[v[j]] = v[(j + fullOb) % v.size()];
}
for (i=0;i<n;i++) for (j=0;j<m;j++) if (tab[i][j]) tab[i][j] = newVal[tab[i][j]];
rest = amoves % 4;
while (rest--) {
	makeMove(q[ind]);
	ind = (ind + 1) % 4;
}
print();
return 0;
}