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
#include "bits/stdc++.h" // Ignacy Boehlke
using namespace std;     // XIII LO Szczecin
template<class A,class B>ostream&operator<<(ostream&os,pair<A,B>p){return os<<'{'<<p.first<<", "<<p.second<<'}';}
template<class T>auto operator<<(ostream&os,T v)->decltype(v.end(),os){os<<'{';int i=0;for(auto e:v)os<<(", ")+2*!i++<<e;return os<<'}';}
#ifdef DEBUG
#define LOG(x...)cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x)
#else
#define LOG(...)(void)0
#endif
#define ssize(x)((int)x.size())
#define FOR(a,b,c)for(int a=(b);a<=(c);a++)
#define REP(a,b)FOR(a,0,(b)-1)
#define ALL(x)(x).begin(), (x).end()
#define fi first
#define se second
using ll=long long;

using Board=vector<vector<int>>;

//constexpr char opp[] = {['D'] = 'G', ['G'] = 'D', ['L'] = 'P', ['P'] = 'L'};
//constexpr bool vert[] = {['D'] = true, ['G'] = true, ['L'] = false, ['P'] = false};
// :(
char opp[100];
char vert[100];
void init() {
	opp['D'] = 'G', opp['G'] = 'D', opp['L'] = 'P', opp['P'] = 'L';
	vert['D'] = vert['G'] = true, vert['L'] = vert['P'] = false;
}

int n, m;
void sim(Board& b, char d) {
	switch (d) {
	case 'G':
		REP(y, n) REP(x, m) {
			bool f = false;
			FOR(dy, 1, y) {
				if (b[y - dy][x] != -1) {
					swap(b[y][x], b[y - dy + 1][x]);
					f = true;
					break;
				}
			}
			if (!f) swap(b[y][x], b[0][x]);
		}
		break;
	case 'D':
		for (int y = n - 1; y >= 0; y--) REP(x, m) {
			bool f = false;
			FOR(dy, 1, n - y - 1) {
				if (b[y + dy][x] != -1) {
					swap(b[y][x], b[y + dy - 1][x]);
					f = true;
					break;
				}
			}
			if (!f) swap(b[y][x], b[n - 1][x]);
		}
		break;
	case 'L':
		REP(x, m) REP(y, n) {
			bool f = false;
			FOR(dx, 1, x) {
				if (b[y][x - dx] != -1) {
					swap(b[y][x], b[y][x - dx + 1]);
					f = true;
					break;
				}
			}
			if (!f) swap(b[y][x], b[y][0]);
		}
		break;
	case 'P':
		for (int x = m - 1; x >= 0; x--) REP(y, n) {
			bool f = false;
			FOR(dx, 1, m - x - 1) {
				if (b[y][x + dx] != -1) {
					swap(b[y][x], b[y][x + dx - 1]);
					f = true;
					break;
				}
			}
			if (!f) swap(b[y][x], b[y][m - 1]);
		}
		break;
	}
}

void print(const Board& b) {
	REP(i, n) {
		REP(j, m) cout << (b[i][j] == -1 ? '.' : (char)('B' + b[i][j]));
		cout << '\n';
	}
}

int main() {
	init();
	cin.tie(0)->sync_with_stdio(0);

	cin >> n >> m;

	Board board(n, vector<int>(m));
	REP(i, n) REP(j, m) {
		char c;
		cin >> c;
		board[i][j] = (c == '.' ? -1 : c - 'B');
	}

	int Q;
	cin >> Q;
	string s;
	REP(j, Q) {
		char c;
		cin >> c;
		if (!s.empty() && s.back() == opp[c]) s.pop_back();

		bool ok = true;
		for (int i = ssize(s) - 1; i >= 0; i--) {
			if (vert[s[i]] == vert[c]) {
				if (s[i] == c) ok = false;
				break;
			}
		}
		if (ok) s += c;
		//LOG(s);
	}
#ifdef DEBUG
	cerr << "s: ";
	REP(i, ssize(s)) cerr << s[i];
	cerr << '\n';
#endif
	int N = ssize(s);
	//cout << ssize(s) << '\n' << s << '\n';
	if (N < 8) {
		for (char c : s) sim(board, c);
		print(board);
		return 0;
	}
	int x = 4 + (N % 4);
	REP(i, x) sim(board, s[i]);

	int cnt = (N - 4) / 4;
	s = s.substr(x, 4);

	Board bef = board;
	int nr = 0;
	REP(i, n) REP(j, m) if (bef[i][j] != -1) {
		bef[i][j] = nr++;
	}
	Board aft = bef;
	for (char c : s) sim(aft, c);

	// pos - jaką literę reprezentuje każda liczba
	vector<int> aftp(nr), to(n * m);
	REP(i, n) REP(j, m) if (aft[i][j] != -1) aftp[aft[i][j]] = m * i + j;
	REP(i, n) REP(j, m) if (bef[i][j] != -1) to[m * i + j] = aftp[bef[i][j]]; 

	vector<bool> vis(n * m);
	vector<int> where(n * m);
	REP(i, n * m) if (!vis[i] && bef[i / m][i % m] != -1) {
		vector<int> cyc = {i};
		vis[i] = true;
		int v = to[i];
		while (v != i) {
			vis[v] = true;
			cyc.push_back(v);
			v = to[v];
		}
		int l = cnt % ssize(cyc);
		REP(j, ssize(cyc)) where[cyc[j]] = cyc[(j + l) % ssize(cyc)];
	}

	Board res(n, vector<int>(m, -1));
	REP(i, n) REP(j, m) if (bef[i][j] != -1) {
		int p = where[i * m + j];
		res[p / m][p % m] = board[i][j];
	}

	print(res);

	LOG(cnt); LOG(bef); LOG(aft);
}