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
#include <algorithm>
#include <iostream>
#include <vector>

using board_t = std::vector< char >;

struct Board {
	int wys;
	int szer;
	board_t dt;

	void tilt(char c);
	void clockwise();
};

struct Cmp {
	bool operator() (char const a, char const b) {
		int left = (a == '.') ? 1 : 0;
		int rght = (b == '.') ? 1 : 0;
		return left < rght;
	}
};

void Board::clockwise() {
	board_t tmp(dt.size(), '?');

	for(int i = 0; i < dt.size(); ++i) {
		int wiersz = i / szer;
		int kolmna = i % szer;

		int tr_wiersz = kolmna;
		int tr_kolmna = wys - wiersz - 1;
		tmp[tr_wiersz * wys + tr_kolmna] = dt[i];
	}

	std::swap(szer, wys);
	std::copy(tmp.begin(), tmp.end(), dt.begin());
}

void Board::tilt(char kier) {
	if(kier == 'L') {
		for(int i = 0; i < wys; ++i) {
			auto it = dt.begin() + i * szer;
			std::stable_sort(it, it + szer, Cmp{});
		}
	}
	else if(kier == 'P') {
		for(int i = 0; i < wys; ++i) {
			auto it = dt.rbegin() + i * szer;
			std::stable_sort(it, it + szer, Cmp{});
		}
	}
	else {
		clockwise();

		if(kier == 'D') {
			tilt('L');
		}
		else if(kier == 'G') {
			tilt('P');
		}

		clockwise(); clockwise(); clockwise();
	}
}

int main() {
	Board brd;

	std::cin >> brd.wys >> brd.szer;

	brd.dt.resize(brd.szer * brd.wys);

	for(int i = 0; i < brd.wys; ++i) {
		for(int k = 0; k < brd.szer; ++k) {
			std::cin >> brd.dt[i * brd.szer + k];
		}
	}

	int moves; std::cin >> moves;
	for (int i = 0; i < moves; ++i) {
		char kier; std::cin >> kier;
		brd.tilt(kier);
	}

	for(int i = 0; i < brd.wys; ++i) {
		for(int k = 0; k < brd.szer; ++k) {
			std::cout << brd.dt[i * brd.szer + k];
		}
		std::cout << "\n";
	}
}