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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <bits/stdc++.h>
 
using namespace std;
 
#define endl '\n'
#define L long long
#define MP make_pair
#define REP(i, n) for(int i = 0; i < n; ++i)
#define REPR(i, n) for(int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define FORR(i, a, b) for(int i = b - 1; i >= a; --i)
#define EB emplace_back
#define ST first
#define ND second
#define S size
#define RS resize
 
template<class T> using P = pair<T, T>;
template<class T> using V = vector<T>;

int n, m, k;
string ks;

int char_to_int(char c) {
    if (c == 'B') return 1;
    else if (c == 'C') return 2;
    else return 0;
}

char int_to_char(int x) {
    if (x == 1) return 'B';
    else if (x == 2) return 'C';
    else return '.';
}

void print_grid(V<V<int>>& grid) {
    REP(y, n) {
        REP(x, m) {
            cout << int_to_char(grid[y][x]);
        }
        cout << '\n';
    }
}

int dim(char c) {
    return (int)(c == 'L' || c == 'P');
}

int first_2_dim() {
    REP(i, k - 1) {
        if (dim(ks[i]) != dim(ks[i + 1])) {
            return i;
        }
    }
    return -1;
}

int get_state(char c1, char c2) {
    if (c1 > c2) swap(c1, c2);
    if (c1 == 'G' && c2 == 'P') return 0;
    if (c1 == 'G' && c2 == 'L') return 1;
    if (c1 == 'D' && c2 == 'L') return 2;
    return 3;
}

int get_value(int state, char c) {
    if (state == 0 && c == 'L') return 1;
    if (state == 0 && c == 'D') return -1;
    if (state == 1 && c == 'D') return 1;
    if (state == 1 && c == 'P') return -1;
    if (state == 2 && c == 'P') return 1;
    if (state == 2 && c == 'G') return -1;
    if (state == 3 && c == 'G') return 1;
    if (state == 3 && c == 'L') return -1;
    return 0;
}

int compute_ks_value(int state, int start) {
    int value = 0;
    FOR(i, start, k) {
        value += get_value(state, ks[i]);
        state = (state + get_value(state, ks[i]) + 4) % 4;
    }
    return value;
}

void move_left(V<V<int>>& grid) {
    REP(y, n) {
        int pos = 0;
        REP(x, m) {
            if (grid[y][x] != 0) {
                grid[y][pos] = grid[y][x];
                if (pos != x) grid[y][x] = 0;
                pos++;
            }
        }
    }
}

void move_right(V<V<int>>& grid) {
    REP(y, n) {
        int pos = m - 1;
        REPR(x, m) {
            if (grid[y][x] != 0) {
                grid[y][pos] = grid[y][x];
                if (pos != x) grid[y][x] = 0;
                pos--;
            }
        }
    }
}

void move_up(V<V<int>>& grid) {
    REP(x, m) {
        int pos = 0;
        REP(y, n) {
            if (grid[y][x] != 0) {
                grid[pos][x] = grid[y][x];
                if (pos != y) grid[y][x] = 0;
                pos++;
            }
        }
    }
}

void move_down(V<V<int>>& grid) {
    REP(x, m) {
        int pos = n - 1;
        REPR(y, n) {
            if (grid[y][x] != 0) {
                grid[pos][x] = grid[y][x];
                if (pos != y) grid[y][x] = 0;
                pos--;
            }
        }
    }
}

void move_brutally(V<V<int>>& grid, char c) {
    if (c == 'L') move_left(grid);
    else if (c == 'P') move_right(grid);
    else if (c == 'G') move_up(grid);
    else move_down(grid);
}

void walk(V<V<int>>& grid, int moves, int state, int sign) {
    REP(i, moves) {
        if (state == 0 && sign == 1) move_left(grid);
        if (state == 0 && sign == -1) move_down(grid);
        if (state == 1 && sign == 1) move_down(grid);
        if (state == 1 && sign == -1) move_right(grid);
        if (state == 2 && sign == 1) move_right(grid);
        if (state == 2 && sign == -1) move_up(grid);
        if (state == 3 && sign == 1) move_up(grid);
        if (state == 3 && sign == -1) move_left(grid);
        state = (state + sign + 4) % 4;
    }
}

V<V<int>> get_ints(V<V<int>>& grid) {
    V<V<int>> ints(n, V<int>(m));
    int v = 1;
    REP(y, n) {
        REP(x, m) {
            if (grid[y][x] != 0) {
                ints[y][x] = v;
                v++;
            }
        }
    }
    return ints;
}

int compute_log(int x) {
    int res = 1;
    while (x > 0) {
        res++;
        x /= 2;
    }
    return res;
}

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

    cin >> n >> m;
    V<V<int>> grid(n, V<int>(m));
    REP(i, n) {
        REP(j, m) {
            char c;
            cin >> c;
            grid[i][j] = char_to_int(c);
        }
    }
    cin >> k >> ks;

    int start = first_2_dim();
    if (start == -1) {
        move_brutally(grid, ks[k - 1]);
        print_grid(grid);
        return 0;
    }

    move_brutally(grid, ks[start]);
    move_brutally(grid, ks[start + 1]);

    int state = get_state(ks[start], ks[start + 1]);
    int ks_value = compute_ks_value(state, start + 2);

    if (ks_value == 0) {
        print_grid(grid);
        return 0;
    }

    int cycles = abs(ks_value) / 4;
    int sign = ks_value > 0 ? 1 : -1;
    int rest = abs(ks_value) % 4;

    if (cycles == 0) {
        walk(grid, rest, state, sign);
        print_grid(grid);
        return 0;
    }

    V<V<int>> ints = get_ints(grid);
    V<V<int>> ints2 = ints;
    walk(ints2, 4, state, sign);
    V<P<int>> ints2_rev(n * m + 1, MP(-1, -1));
    REP(y, n) {
        REP(x, m) {
            if (ints2[y][x] != 0) {
                ints2_rev[ints2[y][x]] = MP(y, x);
            }
        }
    }
    int l = compute_log(cycles);
    V<V<V<P<int>>>> jp(l, V<V<P<int>>>(n, V<P<int>>(m)));
    REP(y, n) {
        REP(x, m) {
            if (ints[y][x] != 0) {
                jp[0][y][x] = ints2_rev[ints[y][x]];
            }
        }
    }
    FOR(i, 1, l) {
        REP(y, n) {
            REP(x, m) {
                if (ints[y][x] != 0) {
                    P<int> jump = jp[i - 1][y][x];
                    jp[i][y][x] = jp[i - 1][jump.ST][jump.ND];
                }
            }
        }
    }
    REP(i, l) {
        if (cycles & (1 << i)) {
            V<V<int>> new_grid(n, V<int>(m));
            REP(y, n) {
                REP(x, m) {
                    if (grid[y][x] != 0) {
                        P<int> jump = jp[i][y][x];
                        new_grid[jump.ST][jump.ND] = grid[y][x];
                    }
                }
            }
            grid = new_grid;
        }
    }

    walk(grid, rest, state, sign);
    print_grid(grid);
}

// g++ -std=c++17 -Wall -Wextra -Wshadow -Wconversion -D_GLIBCXX_DEBUG lam.cpp -o lam