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
//============================================================================
// Name        : 4c-lam.cpp
// Author      : poz
// Version     :
// Description : https://sio2.mimuw.edu.pl/c/pa-2022-1/p/
//============================================================================

#include <bits/stdc++.h>
using namespace std;

const char K = '.';
const char B = 'B';
const char C = 'C';

const char G = 'G';
const char D = 'D';
const char L = 'L';
const char P = 'P';

void printBoard(vector<vector<char>> &board) {
	for (size_t i = 0; i < board.size(); i++) {
		for (size_t j = 0; j < board[0].size(); j++) {
			cout << board[i][j];
		}
		cout << endl;
	}
}

void moveRowLeft(vector<char> &row, const int width) {
	int free = -1;
	for (int i = 0; i < width; i++) {
		if (row[i] != K && free >= 0) {
			row[free] = row[i];
			row[i] = K;
			free++;
		} else if (row[i] == K && free < 0) {
			free = i;
		}
	}
}

void moveRowRight(vector<char> &row, const int width) {
	int free = -1;
	for (int i = width - 1; i >= 0; i--) {
		if (row[i] != K && free >= 0) {
			row[free] = row[i];
			row[i] = K;
			free--;
		} else if (row[i] == K && free < 0) {
			free = i;
		}
	}
}

void moveColTop(vector<vector<char>> &board, const int col, const int height) {
	int free = -1;
	for (int i = 0; i < height; i++) {
		if (board[i][col] != K && free >= 0) {
			board[free][col] = board[i][col];
			board[i][col] = K;
			free++;
		} else if (board[i][col] == K && free < 0) {
			free = i;
		}
	}
}

void moveColDown(vector<vector<char>> &board, const int col, const int height) {
	int free = -1;
	for (int i = height - 1; i >= 0; i--) {
		if (board[i][col] != K && free >= 0) {
			board[free][col] = board[i][col];
			board[i][col] = K;
			free--;
		} else if (board[i][col] == K && free < 0) {
			free = i;
		}
	}
}

void moveBlocks(char mov, vector<vector<char>> &board, const int H,
		const int W) {
	if (mov == G) {
		for (int col = 0; col < W; col++) {
			moveColTop(board, col, H);
		}
	} else if (mov == D) {
		for (int col = 0; col < W; col++) {
			moveColDown(board, col, H);
		}
	} else if (mov == L) {
		for (int row = 0; row < H; row++) {
			moveRowLeft(board[row], W);
		}
	} else if (mov == P) {
		for (int row = 0; row < H; row++) {
			moveRowRight(board[row], W);
		}
	}
}

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

	string movements;
	int n, m, k;
	cin >> n;
	cin >> m;
	cin.ignore();

	vector<vector<char>> board(n, vector<char>(m));
	string line;
	for (int i = 0; i < n; i++) {
		cin >> line;
		for (int j = 0; j < m; j++) {
			board[i][j] = line[j];
		}
	}
	cin >> k;
	cin.ignore();
	cin >> movements;

	for (size_t i = 0; i < movements.size(); i++) {
		char movement = movements[i];
		moveBlocks(movement, board, n, m);

	}

	printBoard(board);
	return 0;
}