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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Karol Kosinski 2022
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i)
#define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i)
#define ALL(c) (c).begin(),(c).end()
#define SIZE(c) int((c).size())
#define TIE(x...) int x;tie(x)
#define X first
#define Y second
#ifndef ENABLE_DEBUG
#define DEB(k,p,f,x...)
#else
#define DEB(k,p,f,x...) {if(k)printf("--------%4d : %s\n",__LINE__,__FUNCTION__);if(p)f(x);}
#endif
#define DEBL DEB(1,1,void,0)
#define DEBF(f,x...) DEB(1,1,f,x)
#define DEBC(p,x...) DEB(0,p,printf,x)
#define DEBUG(x...) DEB(0,1,printf,x)
using namespace std;
using LL = long long;
using ULL = unsigned long long;
using PII = pair<int, int>;
using TIII = tuple<int, int, int>;

constexpr int NX = 505;
constexpr int KX = 500'005;

char A[NX][NX], M[KX], BC[ NX * NX ];
int E[NX][NX];
PII Pos[ NX * NX ], PermPos[ NX * NX ];
bool Was[ NX * NX ];
vector<vector<int>> Perm;
vector<int> Tmp;

void print_board(int n, int m)
{
    FOR(i,0,n)
    {
        printf("%s\n", A[i]);
    }
}

void print_E(int n, int m)
{
    FOR(i,0,n)
    {
        FOR(j,0,m)
        {
            if ( E[i][j] )
            {
                DEBUG(" %2d", E[i][j]);
            }
            else
            {
                DEBUG("  .");
            }
        }
        DEBUG("\n");
    }
}

void move_G(int n, int m)
{
    FOR(j,0,m)
    {
        FOR(i,0,n) if ( E[i][j] ) Tmp.push_back(i);
        int fill = 0;
        for ( int v : Tmp )
        {
            swap( E[v][j], E[fill][j] );
            ++ fill;
        }
        Tmp.clear();
    }
}

void move_D(int n, int m)
{
    FOR(j,0,m)
    {
        FD_(i,n-1,0) if ( E[i][j] ) Tmp.push_back(i);
        int fill = n - 1;
        for ( int v : Tmp )
        {
            swap( E[v][j], E[fill][j] );
            -- fill;
        }
        Tmp.clear();
    }
}

void move_L(int n, int m)
{
    FOR(i,0,n)
    {
        FOR(j,0,m) if ( E[i][j] ) Tmp.push_back(j);
        int fill = 0;
        for ( int v : Tmp )
        {
            swap( E[i][v], E[i][fill] );
            ++ fill;
        }
        Tmp.clear();
    }
}

void move_P(int n, int m)
{
    FOR(i,0,n)
    {
        FD_(j,m-1,0) if ( E[i][j] ) Tmp.push_back(j);
        int fill = m - 1;
        for ( int v : Tmp )
        {
            swap( E[i][v], E[i][fill] );
            -- fill;
        }
        Tmp.clear();
    }
}

int encode(int n, int m)
{
    int next = 1;
    FOR(i,0,n)
    {
        FOR(j,0,m)
        {
            switch ( A[i][j] )
            {
                case '.': E[i][j] = 0; break;
                case 'B': E[i][j] = next; BC[ next ++ ] = 'B'; break;
                default : E[i][j] = next; BC[ next ++ ] = 'C'; break;
            }
        }
    }
    return -- next;
}

void decode(int n, int m)
{
    FOR(i,0,n)
    {
        FOR(j,0,m)
        {
            if ( E[i][j] == 0 ) A[i][j] = '.';
            else A[i][j] = BC[ E[i][j] ];
        }
    }
}

void take_positions(int n, int m)
{
    FOR(i,0,n)
    {
        FOR(j,0,m)
        {
            int d = E[i][j];
            if (d) Pos[d] = make_pair(i, j);
        }
    }
}

// void take_snapshot(int n, int m)
// {
    // FOR(i,0,n)
    // {
        // FOR(j,0,m)
        // {
            // F[i][j] = E[i][j];
        // }
    // }
// }

int to_id(char ch)
{
    switch (ch)
    {
        case 'G': return 0;
        case 'D': return 1;
        case 'L': return 2;
        case 'P': return 3;
    }
}

struct Edge
{
    Edge(int d, int v) : dir(d), val(v) {}
    Edge(int d) : Edge(d, 0) {}
    int dir;
    int val;
};

struct State
{
    char dir_h, dir_v;
    Edge toState[4];
};

State States[4] =
{
    { 'L', 'G', { Edge(0), Edge(3, -1), Edge(0), Edge(1, +1) } },
    { 'P', 'G', { Edge(1), Edge(2, +1), Edge(0, -1), Edge(1) } },
    { 'P', 'D', { Edge(1, -1), Edge(2), Edge(3, +1), Edge(2) } },
    { 'L', 'D', { Edge(0, +1), Edge(3), Edge(3), Edge(2, -1) } },
};

int get_state(int alfa, int beta)
{
    FOR(i,0,4)
    {
        if ( States[i].dir_h == alfa and States[i].dir_v == beta ) return i;
    }
    return -1; // unreachable
}

tuple<int, int, int, int, int> reduce(int k)
{
    int it = 0;
    if ( M[it] == 'L' or M[it] == 'P' )
    {
        while ( M[it] == 'L' or M[it] == 'P' ) ++ it;
    }
    else if ( M[it] == 'G' or M[it] == 'D' )
    {
        while ( M[it] == 'G' or M[it] == 'D' ) ++ it;
    }
    if ( it == k )
    {
        return make_tuple( M[ it - 1 ], 0, 0, 0, 0 );
    }
    int alfa = max( M[ it - 1 ], M[it] );
    int beta = min( M[ it - 1 ], M[it] );
    int cur_state = get_state(alfa, beta), sum = 0;
    FOR( j, it + 1, k )
    {
        auto& ed = States[cur_state].toState[ to_id( M[j] ) ];
        sum += ed.val;
        cur_state = ed.dir;
    }
    return make_tuple( M[ it - 1 ], M[it], States[cur_state].dir_h , States[cur_state].dir_v, sum );
}

void move(int n, int m, char ch)
{
    switch (ch)
    {
        case 'G': move_G(n, m); break;
        case 'D': move_D(n, m); break;
        case 'L': move_L(n, m); break;
        case 'P': move_P(n, m); break;
    }
}

void move_when_0(int n, int m, int start1, int start2)
{
    move(n, m, (char)start1);
    move(n, m, (char)start2);
}

void print_perm(int p)
{
    for ( const auto& vec : Perm )
    {
        for ( int v : vec )
        {
            DEBUG(" %2d", v);
        }
        DEBUG("\n");
    }
    FR_(i,1,p)
    {
        DEBUG("PermPos[%d] = (%2d, %2d)\n", i, PermPos[i].X, PermPos[i].Y);
    }
}

void run_permutations(int n, int m, int p, int seg)
{
    int row = 0;
    FR_(i,1,p)
    {
        if ( Was[i] ) continue;
        int j = i, col = 0;
        Perm.emplace_back();
        do {
            Was[j] = true;
            Perm[row].push_back(j);
            PermPos[j] = make_pair( row, col );
            TIE(xx, yy) = Pos[j];
            j = E[xx][yy];
            ++ col;
        }
        while ( i != j );
        ++ row;
    }
    DEBF(print_perm, p);
    FOR(i,0,n)
    {
        FOR(j,0,m)
        {
            int d = E[i][j];
            if ( d == 0 ) continue;
            TIE(row, col) = PermPos[d];
            int new_col = ( col + seg ) % SIZE( Perm[row] );
            E[i][j] = Perm[row][new_col];
        }
    }
    DEBF(print_E, n, m);
}

void move_when_pos(int n, int m, int first_state, int finish1, int finish2, int p, int sum)
{
    int seg = sum / 4, off = sum % 4;
    char next_move[4] = { 'P', 'D', 'L', 'G' };
    DEBF(print_E, n, m);
    FOR(i,0,off)
    {
        move( n, m, next_move[ ( first_state + i ) % 4 ] );
        DEBF(print_E, n, m);
    }
    if ( seg == 0 ) return;
    take_positions(n, m);
    FOR(i,0,4)
    {
        move( n, m, next_move[ ( first_state + off + i ) % 4 ] );
        DEBF(print_E, n, m);
    }
    if ( seg == 1 ) return;
    run_permutations( n, m, p, seg - 1 );
}

void move_when_neg(int n, int m, int first_state, int finish1, int finish2, int p, int sum)
{
    int seg = sum / 4, off = sum % 4;
    char next_move[4] = { 'D', 'L', 'G', 'P' };
    DEBF(print_E, n, m);
    FOR(i,0,off)
    {
        int i1 = 4 - i;
        move( n, m, next_move[ ( first_state + i1 ) % 4 ] );
        DEBF(print_E, n, m);
    }
    if ( seg == 0 ) return;
    take_positions(n, m);
    FOR(i,0,4)
    {
        int i1 = 8 - off - i;
        move( n, m, next_move[ ( first_state + i1 ) % 4 ] );
        DEBF(print_E, n, m);
    }
    if ( seg == 1 ) return;
    run_permutations( n, m, p, seg - 1 );
}

int main()
{
    int n, m; scanf("%d%d", &n, &m);
    FOR(i,0,n) scanf("%s", A[i]);
    int k; scanf("%d%s", &k, M);
    int p = encode(n, m);
    DEBUG("** encoded: %d\n", p);
    TIE(start1, start2, finish1, finish2, sum) = reduce(k);
    DEBUG("** %c%c%c%c x %d\n", start1, start2, finish1, finish2, sum);
    
    move_when_0(n, m, start1, start2);
    int alfa = max( start1, start2 );
    int beta = min( start1, start2 );
    int first_state = get_state(alfa, beta);
    if ( sum > 0 )
    {
        move_when_pos(n, m, first_state, finish1, finish2, p, sum);
    }
    else if ( sum < 0 )
    {
        move_when_neg(n, m, first_state, finish1, finish2, p, abs(sum));
    }
    decode(n, m);
    print_board(n, m);
    return 0;
}