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
#include <iostream>
#include <string>

int main()
{
    int n, m; std::cin >> n >> m;
    char A[n][m];

    for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
    {
        std::cin >> A[i][j];
    }

    int _; std::cin >> _;
    std::string text; std::cin >> text;

    char pl = ' ';
    char gd = ' ';

    for (int i = 0; i < text.size(); i++)
    {
        if (text[i] == 'P') {
            if (pl == 'P' || (i != text.size() - 1 && (text[i + 1] == 'L' || text[i + 1] == 'P'))) continue;
            for (int y = 0; y < n; y++)
            {
                int counter = 0;
                for (int x = m - 1; x >= 0; x--)
                {
                    if (A[y][x] == '.') continue;

                    A[y][m - 1 - counter] = A[y][x];
                    if (m - 1 - counter != x) A[y][x] = '.';
                    counter++;
                }
            }
            pl = 'P';
        }
        else if (text[i] == 'L') {
            if (pl == 'L' || (i != text.size() - 1 && (text[i + 1] == 'P' || text[i + 1] == 'L'))) continue;
            for (int y = 0; y < n; y++)
            {
                int counter = 0;
                for (int x = 0; x < m; x++)
                {
                    if (A[y][x] == '.') continue;

                    A[y][counter] = A[y][x];
                    if (counter != x) A[y][x] = '.';
                    counter++;
                }
            }
            pl = 'L';
        }
        else if (text[i] == 'D') {
            if (gd == 'D' || (i != text.size() - 1 && (text[i + 1] == 'G' || text[i + 1] == 'D'))) continue;
            for (int x = 0; x < m; x++)
            {
                int counter = 0;
                for (int y = n - 1; y >= 0; y--)
                {
                    if (A[y][x] == '.') continue;

                    A[n - 1 - counter][x] = A[y][x];
                    if (n - 1 - counter != y) A[y][x] = '.';
                    counter++;
                }
            }
            gd = 'D';
        }
        else if (text[i] == 'G') {
            if (gd == 'G' || (i != text.size() - 1 && (text[i + 1] == 'D' || text[i + 1] == 'G'))) continue;
            for (int x = 0; x < m; x++)
            {
                int counter = 0;
                for (int y = 0; y < n; y++)
                {
                    if (A[y][x] == '.') continue;

                    A[counter][x] = A[y][x];
                    if (counter != y) A[y][x] = '.';
                    counter++;
                }
            }
            gd = 'G';
        }
    }

    for (int y = 0; y < n; y++)
    {
        for (int x = 0; x < m; x++)
            std::cout << A[y][x];
        std::cout << "\n";
    }
    
    return 0;
}