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
#include <iostream>
#include <set>
#include <unordered_set>
#include <vector>
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <limits>

using u8 = std::uint8_t;
using u64 = std::uint64_t;
const u64 U64_MAX = std::numeric_limits<std::uint64_t>::max();

int main() {
	u64 n, m;
	std::cin >> n >> m;

	std::vector<std::vector<char>> board(n, std::vector<char>(m, 0));
	for(u64 y = 0; y < n; ++y) {
		for(u64 x = 0; x < m; ++x) {
			char c; std::cin >> c;
			if(c != '.')
				board[y][x] = c;
		}
	}

	enum class direction : u8 { up = 0, down = 1, left = 2, right = 3 };

	auto slide = [&](direction dir) {
		switch(dir) {
		case direction::up:
			for(u64 x = 0; x < m; ++x)
				for(u64 y = 1, cy = 0; y < n; ++y)
					if(board[y][x]) {
						while(board[cy][x])
							++cy, y = std::max(y, cy);
						board[cy++][x] = board[y][x];
						board[y][x] = 0;
					}
			break;
		case direction::down:
			for(u64 x = 0; x < m; ++x)
				for(u64 y = n - 2, cy = n - 1; y != -1; --y)
					if(board[y][x]) {
						while(board[cy][x])
							--cy, y = std::min(y, cy);
						board[cy--][x] = board[y][x];
						board[y][x] = 0;
					}
			break;
		case direction::left:
			for(u64 y = 0; y < n; ++y)
				for(u64 x = 1, cx = 0; x < m; ++x)
					if(board[y][x]) {
						while(board[y][cx])
							++cx, x = std::max(x, cx);
						board[y][cx++] = board[y][x];
						board[y][x] = 0;
					}
			break;
		case direction::right:
			for(u64 y = 0; y < n; ++y)
				for(u64 x = m - 2, cx = m - 1; x != -1; --x)
					if(board[y][x]) {
						while(board[y][cx])
							--cx, x = std::min(x, cx);
						board[y][cx--] = board[y][x];
						board[y][x] = 0;
					}
			break;
		}
	};

	auto print = [&board](std::ostream &out) {
		for(auto &row : board) {
			for(auto &cell : row) {
				if(cell)
					out << cell;
				else out << '.';
			}
			out << '\n';
		}
	};

	u64 inc; std::cin >> inc;
	std::vector<direction> in(inc);
	for(auto &x : in) {
		char c; std::cin >> c;
		switch(c) {
		case 'G':
			x = direction::up;
			break;
		case 'D':
			x = direction::down;
			break;
		case 'L':
			x = direction::left;
			break;
		case 'P':
			x = direction::right;
			break;
		default: assert(false);
		}
	}

	{
		std::unordered_set<direction> todo {
			direction::up,
			direction::down,
			direction::left,
			direction::right,
		};

		for(auto it = in.begin(); it != in.end(); ++it)
			if(auto jt = todo.find(*it); jt != todo.end()) {
				slide(*jt);
				todo.erase(jt);
			}
	}

	{
		std::unordered_set<direction> todo {
			direction::up,
			direction::down,
			direction::left,
			direction::right,
		};

		std::vector<direction> directions;
		for(auto it = in.rbegin(); it != in.rend(); ++it)
			if(auto jt = todo.find(*it); jt != todo.end()) {
				directions.push_back(*jt);
				todo.erase(jt);
			}

		for(auto it = directions.rbegin(); it != directions.rend(); ++it) {
	slide(*it);
}
	}

	print(std::cout);
}