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
#include <bits/stdc++.h>
using namespace std;

int main() {
	int n, m, k, l, p, g, d;
	cin >> n >> m;
	vector <string> lam(n);
	string ruchy;
	//gdpl.. d..p
	vector <bool> r('P' - 'D' + 1);
	stack <char> riuchi;
	for (auto &i: lam)
		cin >> i;
	cin >> k >> ruchy;
	cout << ruchy << endl;
	
	for (auto i{k-1}; i >= 0; --i)
		if (not r[ruchy[i] - 'D']){
			riuchi.push(ruchy[i]);
			r[ruchy[i] - 'D'] = true;
		}
	while (not riuchi.empty()) {
		switch(riuchi.top()){
			case 'G':
					for (k=0; k < m; ++k) {
						g = 0, d = 1;
						while (d < n) {
							if (lam[g][k] != '.') ++g;
							if (g == d) ++d;
							else if (lam[d][k] == '.' )++d;
							else if (lam[g][k] == '.')
								swap (lam[g++][k], lam[d++][k]);
							}
						} 
					
					break;
			case 'D':
					for (k=0; k < m; ++k) {
						g = 1, d = 0;
						while (g >= 0) {
							if (lam[d][k] != '.') --d;
							if (g == d) ++g;
							else if (lam[g][k] == '.' )--g;
							else if (lam[d][k] == '.')
								swap (lam[g--][k], lam[d--][k]);
							}
						} 
					
					break;
			case 'L':
					for(auto & i: lam){
						l = 0, p = 1;
						while (p < m) {
							if (i[l] != '.') ++l;
							if (l >= p) ++p;
							else if (i[p] == '.') ++p;
							else if (i[l] == '.')
								swap (i[l++], i[p++]);
							
							}
						}
					break;
			case 'P':
					for(auto & i: lam){
						l = m-2, p = m-1;
						while (l >= 0) {
							if (i[p] != '.') --p;
							if (p == l) --l;
							else if (i[l] == '.') --l;
							else if (i[p] == '.')
								swap (i[l--], i[p--]);
							}
						}
					
					
					break;
			}
			riuchi.pop();
		}
	
	for (auto i: lam)
			
		cout << i << '\n';
}