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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Element {
	int x, y;
	char color;
};

inline bool
operator < (const Element& a, const Element& b) {
	return a.y == b.y ? a.x < b.x : a.y < b.y;
}

char final_board[512][512];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	int height, width;
	cin >> height >> width;

	vector<Element> elements;
	for(int i = 0; i < height; ++i) {
		string str;
		cin >> str;
		for(int j = 0; j < width; ++j) {
			if(str[j] != '.')
				elements.push_back({j, i, str[j]});
		}
	}

	string str;
	cin >> str >> str;

	for(const char& i : str) {
		vector<int> tmp_vec;
		sort(elements.begin(), elements.end());
		switch(i) {
			case 'G':
				tmp_vec.resize(width, 0);
				for(auto& i : elements)
					i.y = tmp_vec[i.x]++;
				break;
			case 'D':
				tmp_vec.resize(width, height);
				for(auto i = elements.rbegin(); i < elements.rend(); ++i)
					i->y = --tmp_vec[i->x];
				break;
			case 'L':
				tmp_vec.resize(height, 0);
				for(auto& i : elements)
					i.x = tmp_vec[i.y]++;
				break;
			case 'P':
				tmp_vec.resize(height, width);
				for(auto i = elements.rbegin(); i < elements.rend(); ++i)
					i->x = --tmp_vec[i->y];
		}
	}

	for(const auto& i : elements)
		final_board[i.y][i.x] = i.color;

	for(int i = 0; i < height; ++i) {
		for(int j = 0; j < width; ++j)
			cout << (final_board[i][j] ? final_board[i][j] : '.');
		cout << '\n';
	}
}