Niestety, nie byliśmy w stanie w pełni poprawnie wyświetlić tego pliku, ponieważ nie jest zakodowany w UTF-8. Możesz pobrać ten plik i spróbować otworzyć go samodzielnie.
  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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* -----------------------
Autor: Tomasz Boguslawski
-------------------------- */
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<sstream>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include <fstream>
#include<math.h>

#define LL long long
#define FOR(x, b, e) for(LL x = b; x <= (e); x++)
#define FORS(x, b, e, s) for(LL x = b; x <= (e); x+=s)
#define FORD(x, b, e) for(LL x = b; x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define DEBUG if (debug)
#define MIN(a,b) ((a>b)?b:a)
#define MAX(a,b) ((a>b)?a:b)

using namespace std;

LL n,m,k;
string C;

char p[510][510];
char r1[510][510];
char r2[510][510];
char r3[510][510];
LL b[510][510];

void display()
{
    FOR(i,0,n-1)
    {
        FOR(j,0,m-1) cout << p[i][j];
        cout << "\n";
    }
}

void moveL()
{
    FOR(i,0,n-1)
    {
        LL k=0; // next free field to fill
        FOR(j,0,m-1) if (p[i][j]!='.') { p[i][k]=p[i][j]; k++; }
        FOR(j,k,m-1) p[i][j]='.';
    }
}

void moveR()
{
    FOR(i,0,n-1)
    {
        LL k=m-1; // next free field to fill
        FORD(j,m-1,0) if (p[i][j]!='.') { p[i][k]=p[i][j]; k--; }
        FORD(j,k,0) p[i][j]='.';
    }
}

void moveU()
{
    FOR(j,0,m-1)
    {
        LL k=0; // next free field to fill
        FOR(i,0,n-1) if (p[i][j]!='.') { p[k][j]=p[i][j]; k++; }
        FOR(i,k,n-1) p[i][j]='.';
    }
}

void moveD()
{
    FOR(j,0,m-1)
    {
        LL k=n-1; // next free field to fill
        FORD(i,n-1,0) if (p[i][j]!='.') { p[k][j]=p[i][j]; k--; }
        FORD(i,k,0) p[i][j]='.';
    }
}

void moveBL()
{
    FOR(i,0,n-1)
    {
        LL k=0; // next free field to fill
        FOR(j,0,m-1) if (b[i][j]!=-1) { b[i][k]=b[i][j]; k++; }
        FOR(j,k,m-1) b[i][j]=-1;
    }
}

void moveBR()
{
    FOR(i,0,n-1)
    {
        LL k=m-1; // next free field to fill
        FORD(j,m-1,0) if (b[i][j]!=-1) { b[i][k]=b[i][j]; k--; }
        FORD(j,k,0) b[i][j]=-1;
    }
}

void moveBU()
{
    FOR(j,0,m-1)
    {
        LL k=0; // next free field to fill
        FOR(i,0,n-1) if (b[i][j]!=-1) { b[k][j]=b[i][j]; k++; }
        FOR(i,k,n-1) b[i][j]=-1;
    }
}

void moveBD()
{
    FOR(j,0,m-1)
    {
        LL k=n-1; // next free field to fill
        FORD(i,n-1,0) if (b[i][j]!=-1) { b[k][j]=b[i][j]; k--; }
        FORD(i,k,0) b[i][j]=-1;
    }
}


void makeRandomBoard()
{
    FOR(i,0,n-1) FOR(j,0,m-1) p[i][j]='.';
    LL le = 3*(m/4);
    FOR(i,0,n-1)
    {
        if (le<=0) break;
        FOR(j,0,le-1) p[i][j]=(char)(65+rand()%20);
        le-=rand()%5;
    }
}

void makeRandomSeq(int len)
{
    string s(len, ' ');
    FOR(i,0,len-1)
    {
        int x=rand()%4;
        if (x==0) s[i]='G';
        else if (x==1) s[i]='D';
        else if (x==2) s[i]='L';
        else s[i]='P';
    }
    C=s; k=len;
}

void makeMove(char ch)
{
    if (ch=='G') moveU();
    else if (ch=='D') moveD();
    else if (ch=='P') moveR();
    else moveL();
}


void silly()
{
    FOR(i,0,k-1) makeMove(C[i]);
}

void makeRotations(int &corner, int moves)
{
    if (moves>=0)
    {
        // clockwise
        FOR(i,1,moves)
        {
            if (corner==0) { corner=1; moveR(); }
            else if (corner==1) { corner=2; moveD(); }
            else if (corner==2) { corner=3; moveL(); }
            else {corner=0; moveU(); }
        }
    }
    else
    {
        // anticlockwise
        FOR(i,1,-moves)
        {
            if (corner==0) { corner=3; moveD(); }
            else if (corner==1) { corner=0; moveL(); }
            else if (corner==2) { corner=1; moveU(); }
            else {corner=2; moveR(); }
        }
    }
}


LL code(LL i, LL j)
{
    return i*m+j;
}

LL perm[30][300000];
LL pe[300000];

void smart()
{
    LL s=0;
    while (s<=k-1)
    {
        if (C[s]=='L' || C[s]=='P') s++;
        else break;
    }
    if (s==0)
    {
        // there was no L nor P at the beginning
        while (s<=k-1)
        {
            if (C[s]=='G' || C[s]=='D') s++;
            else break;
        }
        if (s>0) s--;
    }
    else
    {
        // there were L or P at the beginning
        s--;
    }
    //cout << "After reduction: ";
    //FOR(i,s,k-1) cout << C[i]; cout << "\n";
    if (s==k-1)
    {
        // there is nothing more
        char ch=C[s];
        if (ch=='G') moveU();
        else if (ch=='D') moveD();
        else if (ch=='P') moveR();
        else moveL();
        //cout << "gotowe!\n";
        return;
    }
    string the2=C.substr(s,2);
    FOR(i,0,1) makeMove(the2[i]);
    s+=2;
    if (s>k-1) return;
    int corner0=0;
    if (the2=="LG"||the2=="GL") corner0=0;
    else if (the2=="PG"||the2=="GP") corner0=1;
    else if (the2=="PD"||the2=="DP") corner0=2;
    else if (the2=="LD"||the2=="DL") corner0=3;
    else cout << "Sth wrong\n";
    //cout << "Initial corner=" << corner0 << "\n";
    LL moves=0; int corner=corner0;
    FOR(i,s,k-1)
    {
        char ch=C[i];
        if (ch=='G')
        {
            if (corner==3) { corner=0; moves++;}
            else if (corner==2) { corner=1; moves--; }
        }
        else if (ch=='D')
        {
            if (corner==0) { corner=3; moves--;}
            else if (corner==1) { corner=2; moves++; }
        }
        else if (ch=='L')
        {
            if (corner==1) { corner=0; moves--;}
            else if (corner==2) { corner=3; moves++; }
        }
        else if (ch=='P')
        {
            if (corner==0) { corner=1; moves++;}
            else if (corner==3) { corner=2; moves--; }
        }
    }
    //cout << "Moves=" << moves << "\n";
    corner=corner0;
    LL absMoves=moves; if (moves<0) absMoves=-moves;
    if (absMoves<10)  // tu ma byc 10 !!!!!!!!!!!!!!!!!!!!!!!!!!
    {
        makeRotations(corner, moves);
        // cout << "gotowe!!\n";
        return;
    }
    while (corner!=0)
    {
        if (moves>0) { makeRotations(corner, 1); moves--; }
        else { makeRotations(corner, -1); moves++; }
    }
    //cout << "Moves left: " << moves << "\n";
    //display();
    // ------ do tego miejsca jest dobrze
    FOR(i,0,n-1) FOR(j,0,m-1)
    {
        if (p[i][j]=='.') b[i][j]=-1;
        else b[i][j]=code(i,j);
        //cout << i << "," << j << " (" << code(i,j) << ") b=" << b[i][j] << "\n";
    }
    if (moves>0) { moveBR(); moveBD(); moveBL(); moveBU(); }
    else { moveBD(); moveBR(); moveBU(); moveBL(); }
    /*
    FOR(i,0,n-1)
    {
        FOR(j,0,m-1) cout << b[i][j] << " ";
        cout << "\n";
    }
    */
    FOR(i,0,n-1) FOR(j,0,m-1)
    {
        if (b[i][j]==-1) perm[0][code(i,j)]=code(i,j);
        else perm[0][b[i][j]]=code(i,j);
    }
    //FOR(i,0,n-1) FOR(j,0,m-1) cout << i << "," << j << " (" << code(i,j) << ") -> " << perm[0][code(i,j)] << "\n";
    LL exp=1; LL lastPot=0;
    absMoves=moves; if (moves<0) absMoves=-moves;
    LL fullCircles=absMoves/4;
    while (exp<fullCircles)
    {
        exp=exp*2; lastPot++;
        FOR(i,0,n-1) FOR(j,0,m-1) perm[lastPot][code(i,j)]=perm[lastPot-1][ perm[lastPot-1][code(i,j)] ];
    }
    //cout << "fullCircles=" << fullCircles << " exp=" << exp << " lastPot=" << lastPot << "\n";
    LL pot=lastPot;
    //FOR(k,0,n*m) pe[k]=k;
    while (abs(moves)>=4)
    {
        while(exp>fullCircles) { exp=exp/2; pot--; };
        // perm[pot] jest permutacja z�o�on� 2^pot razy
        FOR(i,0,n-1) FOR(j,0,m-1)
        {
            if (p[i][j]=='.') r3[i][j]='.';
            else
            {
                LL targetCode=perm[pot][code(i,j)];
                LL i1=targetCode / m; LL j1=targetCode % m;
                r3[i1][j1]=p[i][j];
            }
        }
        FOR(i,0,n-1) FOR(j,0,m-1) p[i][j]=r3[i][j];
        fullCircles-=exp; if (moves>0) moves-=4*exp; else if (moves<0) moves+=4*exp;
    }
    corner=0;
    makeRotations(corner, moves);
}

bool oneTest()
{
    // copy P to R1:
    FOR(i,0,n-1) FOR(j,0,m-1) r1[i][j]=p[i][j];
    silly();
    //cout << "After silly:\n";
    //display();
    // copy P to R2:
    FOR(i,0,n-1) FOR(j,0,m-1) r2[i][j]=p[i][j];
    // copy R1 to P:
    FOR(i,0,n-1) FOR(j,0,m-1) p[i][j]=r1[i][j];
    smart();
    //cout << "After smart:\n";
    //display();
    // compare P with R2:
    FOR(i,0,n-1) FOR(j,0,m-1) if (p[i][j]!=r2[i][j])
    {
        cout << "Error!\n";
        return false;
    }
    return true;
}


/// MAIN
int main(int argc, char* argv[])
{
    // magic formula, which makes streams work faster:
	ios_base::sync_with_stdio(0);

	cin >> n; cin >> m;
	string line;
	FOR(i,0,n-1)
	{
	    cin >> line;
	    FOR(j,0,m-1) p[i][j]=line[j];
	}
    cin >> k;
    cin >> C;
    FOR(i,0,k-1) if (C[i]=='R') C[i]='P';

    smart();
    display();
    /*
    n=23; m=34;
    FOR(z,1,10000)
    {
        makeRandomBoard();
        makeRandomSeq(1+rand()%50);
        cout << "Seq: "; FOR(i,0,k-1) cout << C[i]; cout << "\n";
        bool ok = oneTest();
        if (!ok) return 0;
    }
    cout << "All OK!\n";
    */

 /*
	while(true)
    {
        draw();
        cin >> line;
        if (line=="G") moveU();
        else if (line=="D") moveD();
        else if (line=="L") moveL();
        else if (line=="P") moveR();
        else break;
    }
*/
	return 0;
}