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
#include <cstdio>
#include <cstdlib>
#include <cstring>

#define	MAX_N	3000

static unsigned tab[500][500];
static unsigned h, w;

typedef void op_t(void);
typedef op_t *op_p;

static void op_gora(void)
{
	unsigned x, y, z;

	for (x = 0; x < w; x++) {
		for (y = z = 0; y < h; y++) {
			if (tab[y][x] != '.') {
				tab[z++][x] = tab[y][x];
			}
		}
		while (z < h) {
			tab[z++][x] = '.';
		}
	}
}

static void op_dol(void)
{
	unsigned x, y, z;

	for (x = 0; x < w; x++) {
		for (y = z = h; y > 0; y--) {
			if (tab[y - 1][x] != '.') {
				tab[--z][x] = tab[y - 1][x];
			}
		}
		while (z > 0) {
			tab[--z][x] = '.';
		}
	}
}

static void op_lewo(void)
{
	unsigned x, y, z;

	for (y = 0; y < h; y++) {
		for (x = z = 0; x < h; x++) {
			if (tab[y][x] != '.') {
				tab[y][z++] = tab[y][x];
			}
		}
		while (z < w) {
			tab[y][z++] = '.';
		}
	}
}

static void op_prawo(void)
{
	unsigned x, y, z;

	for (y = 0; y < h; y++) {
		for (x = z = w; x > 0; x--) {
			if (tab[y][x - 1] != '.') {
				tab[y][--z] = tab[y][x - 1];
			}
		}
		while (z > 0) {
			tab[y][--z] = '.';
		}
	}
}

static op_p do_op[0x100];

int main(void)
{
	unsigned x, y, ops;

	do_op['G'] = op_gora;
	do_op['L'] = op_lewo;
	do_op['D'] = op_dol;
	do_op['P'] = op_prawo;

	scanf("%u%u", &h, &w);

	for (y = 0; y < h; y++) {
		getchar();
		for (x = 0; x < w; x++) {
			tab[y][x] = getchar();
		}
	}

	scanf("%u\n", &ops);

	while (ops--) {
		op_p op = do_op[getchar()];

		if (op) {
			op();
		}
	}

	for (y = 0; y < h; y++) {
		for (x = 0; x < w; x++) {
			putchar(tab[y][x]);
		}
		putchar('\n');
	}

	return 0;
}