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
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <bits/stdc++.h>
#define rep(a, b) for (int a = 0; a < b; ++a)
#define repd(a, b) for (int a = b - 1; a >= 0; --a)
#define pb push_back

using namespace std;
typedef long long ll;

const int MAX = 502;

enum Sign {
    C = 0,
    B = 1,
    _ = 2
};

Sign board[MAX][MAX];


void printBoard(int n, int m) {

    rep (i, n) {
        rep (j, m) {
            switch (board[i][j]) {
                case C: cout << 'C'; break;
                case B: cout << 'B'; break;
                default: cout << '.'; break;
            }
        }
        cout << endl;
    }
}

void moveLeft(int n, int m) {
    rep (i, n) {
        ll j = 0;
        ll k = j;
        while (j < m && k < m) {
            while (j < m && board[i][j] != _) j++;
            k = j;
            while (k < m && board[i][k] == _) k++;
            if (j < m && k < m) swap(board[i][j], board[i][k]);
            j++;
        }
    }
}

void moveRight(int n, int m) {
    rep (i, n) {
        ll j = m - 1;
        ll k = j;
        while (j >= 0 && k >= 0) {
            while (j >= 0 && board[i][j] != _) j--;
            k = j;
            while (k >= 0 && board[i][k] == _) k--;
            if (j >= 0 && k >= 0) swap(board[i][j], board[i][k]);
            j--;
        }
    }
}

void moveDown(int n, int m) {
    rep (i, m) {
        ll j = n - 1;
        ll k = j;
        while (j >= 0 && k >= 0) {
            while (j >= 0 && board[j][i] != _) j--;
            k = j;
            while (k >= 0 && board[k][i] == _) k--;
            if (j >= 0 && k >= 0) swap(board[j][i], board[k][i]);
            j--;
        }
    }
}

void moveUp(int n, int m) {
    rep (i, m) {
        ll j = 0;
        ll k = j;
        while (j < n && k < n) {
            while (j < n && board[j][i] != _) j++;
            k = j;
            while (k < n && board[k][i] == _) k++;
            if (j < n && k < n) swap(board[j][i], board[k][i]);
            j--;
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(0);

    ll n, m;
    cin >> n >> m;

    char c;
    rep(i, n) rep(j, m) {
        cin >> c;
        switch (c) {
            case 'C': board[i][j] = C; break;
            case 'B': board[i][j] = B; break;
            case '.': board[i][j] = _; break;
        }
    }

    ll t;
    cin >> t;

    vector<char> tasks(t);
    rep (i, t) cin >> tasks[i];

    vector<char> newTasks;

    ll len = 0;
    do {
        len = tasks.size();
        newTasks.clear();
        char lastLR = '?';
        char lastUD = '?';
        for (int i = 0; i < len; ++i) {

            if (tasks[i] == 'L' || tasks[i] == 'P') {
                while (i < len && (tasks[i] == 'L' || tasks[i] == 'P')) {
                    i++;
                }
                i--;
                if (tasks[i] != lastLR) {
                    newTasks.pb(tasks[i]);
                    lastLR = tasks[i];
                }
            } else {
                while (i < len && (tasks[i] == 'G' || tasks[i] == 'D')) {
                    i++;
                }
                i--;
                if (tasks[i] != lastUD) {
                    newTasks.pb(tasks[i]);
                    lastUD = tasks[i];
                }
            }
        }
        tasks = newTasks;
    } while (newTasks.size() < len);

    rep (i, newTasks.size()) {
        switch (newTasks[i]) {
            case 'G': moveUp(n, m); break;
            case 'D': moveDown(n, m); break;
            case 'L': moveLeft(n, m); break;
            case 'P': moveRight(n, m); break;
        }
    }
    printBoard(n, m);
}