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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <bits/stdc++.h>
using namespace std;template<class a,class b>auto&operator<<(ostream&o,pair<a,b>
x){return o<<'('<<x.first<<' '<<x.second<<')';}template<class t>auto operator<<(
ostream&o,t x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++
<<e;return o<<'}';}auto&operator<<(ostream&o,string x){return o<<x.c_str();};
#ifdef DEBUG
#define LOG(x...)cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x)
#else
#define LOG(x...)0
#endif

int& at(vector<vector<int>> &board, int row, int column, int width,
	 int height, char direction)
{
	switch (direction) {
	case 'D':
		return board[row][column];

	case 'P':
		return board[column][row];

	case 'G':
		return board[height - 1 - row][column];

	case 'L':
		return board[column][width - 1 - row];
	}

	return *(int*)NULL;
}

void print_board(vector<vector<int>> &board)
{
	for (auto &row : board) {
		for (auto &cell : row)
			cout << cell << ' ';

		cout << '\n';
	}
}

void simulate(vector<vector<int>> &board, int height, int width,
	      char direction)
{
	int current_width = width;
	int current_height = height;

	if (direction == 'L' || direction == 'P') 
		swap(current_width, current_height);

	for (int column = 0; column < current_width; column++) {
		int current_row = current_height - 1;

		for (int row = current_row; row >= 0; row--) {
			int& value = at(board, row, column, width, height,
					direction);

			if (value == -1)
				continue;

			if (row != current_row) {
				at(board, current_row, column, width, height,
				   direction) = value;
				value = -1;
			}


			current_row--;
		}
	}
}

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

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

	vector<vector<int>> board(height, vector<int>(width));
	vector<char> values;

	for (int row = 0; row < height; row++) {
		for (int column = 0; column < width; column++) {
			char value;
			cin >> value;

			if (value == '.') {
				board[row][column] = -1;
				continue;
			}

			board[row][column] = values.size();
			values.push_back(value);
		}
	}

	int size;
	cin >> size;

	vector<char> input;

	for (int index = 0; index < size; index++) {
		char current;
		cin >> current;

		if (input.size() == 0) {
			input.push_back(current);
			continue;
		}

		if (current == 'L' || current == 'P') {
			if (input.back() == 'L' || input.back() == 'P')
				input.pop_back();
		} else {
			if (input.back() == 'D' || input.back() == 'G')
				input.pop_back();
		}

		if (input.size() < 2 || input[input.size() - 2] != current)
			input.push_back(current);
	}

	LOG(input);
	
	if (input.size() < 6) {
		for (auto value : input)
			simulate(board, height, width, value);
	} else {
		for (int index = 0; index < 2; index++)
			simulate(board, height, width, input[index]);
		
		vector<vector<int>> original = board;

		for (int index = 2; index < 6; index++)
			simulate(board, height, width, input[index]);

		int rounds = (input.size() - 2) / 4;
		int reminder = (input.size() - 2) % 4;

		vector<pair<int, int>> original_position(values.size());
		vector<bool> visited(values.size());
		vector<pair<int, int>> result(values.size());

		for (int row = 0; row < height; row++) {
			for (int column = 0; column < width; column++) {
				int original_value = original[row][column];

				if (original_value != -1) {
					original_position[original_value] = {
						row,
						column
					};
				}
			}
		}

		for (int index = 0; index < values.size(); index++) {
			if (visited[index])
				continue;

			int loop_length = 0;
			int shift;
			int current = index;
			int row = original_position[index].first;
			int column = original_position[index].second;
			vector<int> loop;
			vector<pair<int, int>> positions;

			while (true) {
				if (board[row][column] == index)
					shift = loop_length;

				visited[current] = true;
				loop.push_back(current);
				positions.push_back({ row, column });
				loop_length++;

				current = board[row][column];
				row = original_position[current].first;
				column = original_position[current].second;

				if (current == index)
					break;
			}
			LOG(loop);
			LOG(positions);

			for (int index = 0; index < loop.size(); index++) {
				int new_index = ((long long)shift * rounds + index)
						% loop.size();
				result[loop[index]] = positions[new_index];
			}

			//LOG(shift);
			//print_board(original);
			//LOG("===");
		}

		LOG(result);

		for (int index = 0; index < result.size(); index++) {
			int row = result[index].first;
			int column = result[index].second;

			board[row][column] = index;
		}

		for (int index = 0; index < reminder; index++) {
			simulate(board, height, width, input[index + 2]);
		}
	}

	for (auto &row : board) {
		for (auto &cell : row) {
			if (cell == -1) {
				cout << '.';
				continue;
			}

			cout << values[cell];
		}

		cout << '\n';
	}

#ifdef DEBUG
	print_board(board);
#endif

}