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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
const ll LINF = 1e18;
const int INF = 1e9;

const int N = 500;
enum Operation { DOWN, LEFT, UP, RIGHT };
enum Cell { EMPTY, WHITE, BLACK };

Operation char_to_op(char c) {
    switch (c) {
    case 'D':
        return DOWN;
    case 'L':
        return LEFT;
    case 'G':
        return UP;
    case 'P':
        return RIGHT;
    default:
        return DOWN;
    }
}

int char_to_cell(char c) {
    switch (c) {
    case '.':
        return EMPTY;
    case 'B':
        return WHITE;
    case 'C':
        return BLACK;
    default:
        return 3;
    }
}

char cell_to_char(int c) {
    switch (c) {
    case EMPTY:
        return '.';
    case WHITE:
        return 'B';
    case BLACK:
        return 'C';
    default:
        return '?';
    }
}

void apply_right(int puzzle[N][N], int n, int m) {
    for (int i = 0; i < n; ++i) {
        int dest_j = m - 1;
        for (int j = m - 1; j >= 0; --j) {
            int c = puzzle[i][j];
            if (c != EMPTY) {
                puzzle[i][j] = EMPTY;
                puzzle[i][dest_j] = c;
                --dest_j;
            }
        }
    }
}

void apply_left(int puzzle[N][N], int n, int m) {
    for (int i = 0; i < n; ++i) {
        int dest_j = 0;
        for (int j = 0; j < m; ++j) {
            int c = puzzle[i][j];
            if (c != EMPTY) {
                puzzle[i][j] = EMPTY;
                puzzle[i][dest_j] = c;
                ++dest_j;
            }
        }
    }
}

void apply_up(int puzzle[N][N], int n, int m) {
    for (int j = 0; j < m; ++j) {
        int dest_i = 0;
        for (int i = 0; i < n; ++i) {
            int c = puzzle[i][j];
            if (c != EMPTY) {
                puzzle[i][j] = EMPTY;
                puzzle[dest_i][j] = c;
                ++dest_i;
            }
        }
    }
}

void apply_down(int puzzle[N][N], int n, int m) {
    for (int j = 0; j < m; ++j) {
        int dest_i = n - 1;
        for (int i = n - 1; i >= 0; --i) {
            int c = puzzle[i][j];
            if (c != EMPTY) {
                puzzle[i][j] = EMPTY;
                puzzle[dest_i][j] = c;
                --dest_i;
            }
        }
    }
}

void apply_op(int puzzle[N][N], int n, int m, Operation op) {
    if (op == RIGHT) {
        apply_right(puzzle, n, m);
    } else if (op == LEFT) {
        apply_left(puzzle, n, m);
    } else if (op == UP) {
        apply_up(puzzle, n, m);
    } else /* op == 'D' */ {
        apply_down(puzzle, n, m);
    }
}

void print_puzzle(int puzzle[N][N], int n, int m) {
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cout << cell_to_char(puzzle[i][j]);
        }
        cout << '\n';
    }
}

bool is_equal(int puzzle1[N][N], int puzzle2[N][N], int n, int m) {
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (puzzle1[i][j] != puzzle2[i][j]) {
                return false;
            }
        }
    }
    return true;
}

vector<vector<pii>> compute_cycles(int puzzle[N][N], int n, int m, int corner) {
    int puzzle_start[N][N];
    int puzzle_next[N][N];

    // Make elements unique
    int id = 1;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            puzzle_start[i][j] = 0;
            if (puzzle[i][j] != EMPTY) {
                puzzle_start[i][j] = id;
                ++id;
            }
            puzzle_next[i][j] = puzzle_start[i][j];
        }
    }

    for (int i = 0; i < 4; ++i) {
        apply_op(puzzle_next, n, m, static_cast<Operation>((corner + i) % 4));
    }

    unordered_map<int, pii> new_coords;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            int e = puzzle_next[i][j];
            if (e != 0) {
                new_coords[e] = {i, j};
            }
        }
    }

    // Compute cycles
    vector<vector<pii>> cycles;
    bool used[n][m];
    memset(used, 0, sizeof(bool) * n * m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (puzzle_start[i][j] != 0 && !used[i][j]) {
                used[i][j] = true;
                cycles.push_back({{i, j}});

                pii start_coords = make_pair(i, j);
                pii next = new_coords[puzzle_start[i][j]];
                while (next != start_coords) {
                    used[next.first][next.second] = true;
                    cycles.back().push_back(next);
                    next = new_coords[puzzle_start[next.first][next.second]];
                }
            }
        }
    }

    return cycles;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int puzzle[N][N];
    // int test_puzzle[N][N];

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            char c;
            cin >> c;
            int cell = char_to_cell(c);
            puzzle[i][j] = cell;
            // test_puzzle[i][j] = cell;
        }
    }

    int k;
    cin >> k;

    vector<Operation> ops(k);
    for (int i = 0; i < k; ++i) {
        char c;
        cin >> c;
        ops[i] = char_to_op(c);
    }

    int ops_to_corner[4][4] = {{-1, 2, -1, 1}, {2, -1, 3, -1}, {-1, 3, -1, 0}, {1, -1, 0, -1}};

    // Skip prefix of symmetric operations
    int op_idx = 1;
    int corner = -1;
    for (; op_idx < k; ++op_idx) {
        if (ops[op_idx] % 2 != ops[0] % 2) {
            apply_op(puzzle, n, m, ops[op_idx - 1]);
            apply_op(puzzle, n, m, ops[op_idx]);
            corner = ops_to_corner[ops[op_idx - 1]][ops[op_idx]];
            break;
        }
    }

    if (op_idx == k) {
        apply_op(puzzle, n, m, ops.back());
        print_puzzle(puzzle, n, m);
        return 0;
    }

    vector<vector<pii>> cycles = compute_cycles(puzzle, n, m, corner);

    // Compute number of 4 * full cycles
    int start_corner = corner;
    int cycle_pos = 0;
    for (int i = op_idx + 1; i < k; ++i) {
        // op to corner difference
        int diff = 0;
        if (ops[i] == corner) {
            diff = 1;
        } else if (ops[i] == (corner + 1) % 4) {
            diff = -1;
        }
        cycle_pos += diff;
        corner += (4 + diff);
        corner %= 4;
    }

    // Create final array
    int result[N][N];
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            result[i][j] = EMPTY;
        }
    }

    int n_cycles  = cycle_pos / 4;
    if (cycle_pos % 4 < 0) {
        --n_cycles;
    }

    for (vector<pii> &cycle : cycles) {
        int cycle_size = static_cast<int>(cycle.size());
        for (int i = 0; i < cycle_size; ++i) {
            pii coords = cycle[i];

            int new_idx = (i + n_cycles) % cycle_size;
            new_idx = new_idx < 0 ? new_idx + cycle_size : new_idx;
            pii new_coords = cycle[new_idx];

            result[new_coords.first][new_coords.second] = puzzle[coords.first][coords.second];
        }
    }

    // Apply suffix ops
    while (start_corner != corner) {
        apply_op(result, n, m, static_cast<Operation>(start_corner));
        start_corner = (start_corner + 1) % 4;
    }

    print_puzzle(result, n, m);

    // For testing
    // for (int i = 0; i < k; ++i) {
    //     apply_op(test_puzzle, n, m, ops[i]);
    // }

    // cout << '\n';
    // if (!is_equal(result, test_puzzle, n, m)) {
    //     cerr << "ERROR! Puzzles not equal, should be:" << endl;
    // }
    // print_puzzle(test_puzzle, n, m);

    return 0;
}