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
#include <bits/stdc++.h>

using namespace std;

short plansza[500][500];
int ilepelno[500];

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

    int n, m, k, x1 = 500, x2 = 0, y1 = 500, y2 = 0, x11, x21, y11, y21;
    char ch;
    string str;
    cin >> n >> m;
    for (int y = 0; y < n; y++)
    {
        for (int x = 0; x < m; x++)
        {
            cin >> ch;
            if (ch != '.')
            {
                if (x < x1) x1 = x;
                if (x > x2) x2 = x;
                if (y < y1) y1 = y;
                if (y > y2) y2 = y;
                if (ch == 'B') plansza[x][y] = 1;
                else plansza[x][y] = 2;
            }
        }
    }

    cin >> k >> str;
    ch = str[0];
    for (int i = 1; i < k; i++) // Usuwanie dwóch takich samych z rzędu
    {
        if (str[i] == ch) str[i] = ' ';
        else ch = str[i];
    }

    for (int i = 0; i < k; i++)
    {
        fill(ilepelno, ilepelno + 500, 0);
        x11 = 500; x21 = 0; y11 = 500; y21 = 0;
        if (str[i] == 'G')
        {
            y11 = 0;
            x11 = x1;
            x21 = x2;
            for (int y = y1; y <= y2; y++)
            {
                for (int x = x1; x <= x2; x++)
                {
                    if (plansza[x][y] > 0)
                    {
                        y21 = max(y21, ilepelno[x]);
                        plansza[x][ilepelno[x]++] = plansza[x][y];
                        plansza[x][y] = 0;
                    }
                }
            }
        }
        else if (str[i] == 'D')
        {
            y21 = n - 1;
            x11 = x1;
            x21 = x2;
            for (int y = n - 1; y >= 0; y--)
            {
                for (int x = 0; x < m; x++)
                {
                    if (plansza[x][y] > 0)
                    {
                        y11 = min(y11, n - 1 - ilepelno[x]);
                        plansza[x][n - 1 - (ilepelno[x]++)] = plansza[x][y];
                        plansza[x][y] = 0;
                    }
                }
            }
        }
        else if (str[i] == 'L')
        {
            x11 = 0;
            y11 = y1;
            y21 = y2;
            for (int x = 0; x < m; x++)
            {
                for (int y = 0; y < n; y++)
                {
                    if (plansza[x][y] > 0)
                    {
                        x21 = max(x21, ilepelno[y]);
                        plansza[ilepelno[y]++][y] = plansza[x][y];
                        plansza[x][y] = 0;
                    }
                }
            }
        }
        else if (str[i] == 'P')
        {
            x21 = m - 1;
            y11 = y1;
            y21 = y2;
            for (int x = m - 1; x >= 0; x--)
            {
                for (int y = 0; y < n; y++)
                {
                    if (plansza[x][y] > 0)
                    {
                        x11 = min(x11, m - 1 - ilepelno[y]);
                        plansza[m - 1 - (ilepelno[y]++)][y] = plansza[x][y];
                        plansza[x][y] = 0;
                    }
                }
            }
        }
        x1 = x11; x2 = x21; y1 = y11; y2 = y21;
    }

    for (int y = 0; y < n; y++)
    {
        for (int x = 0; x < m; x++)
        {
            if (plansza[x][y] == 1) cout << 'B';
            else if (plansza[x][y] == 2) cout << 'C';
            else cout << '.';
        }
        cout << '\n';
    }

    return 0;
}