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
#include <iostream>
#include <limits.h>

using namespace std;

void moveRowLR(char direction, int len, char row[]) {
//    cout << "before "; for (int i=0; i<len; i++) cout << row[i]; cout<<endl;

    int dir = 1;
    int start = 0;
    int stop = len;
    if(direction == 'P'){
        dir = -1;
        start = len - 1;
        stop = -1;
    } 

    int free = start;
    int block = start;
    while(true) {
      while(free != stop && row[free] != '.') free+=dir;
      if (free == stop) break;

      block=(dir == 1 ? max(block, free) : min(block, free));
      while(block != stop && row[block]  == '.') block+=dir;
      if (block == stop) break;

      row[free] = row[block];
      row[block] = '.';
    } 


//    cout << "after  "; for (int i=0; i<len; i++) cout << row[i]; cout<<endl;
}

void moveLR(char direction, int colLen, int rowLen, char* puzzle) {
    for (int i=0; i<colLen; i++)
        moveRowLR(direction, rowLen, (puzzle + rowLen * i));
}

char* getCell(char* tab, int column, int row, int rowLen) {
    return ((tab + row * rowLen) + column);
}

void moveColumnUD(char direction, int len, int rowLen, char* puzzle, int column) {
//    cout << "before\n"; for (int i=0; i<len; i++) cout << *getCell(puzzle, column, i, rowLen) << endl;

    int dir = 1;
    int start = 0;
    int stop = len;
    if(direction == 'D'){
        dir = -1;
        start = len - 1;
        stop = -1;
    } 

    int free = start;
    int block = start;
    while(true) {
      while(free != stop && *getCell(puzzle, column, free, rowLen) != '.') free+=dir;
      if (free == stop) break;

      block=(dir == 1 ? max(block, free) : min(block, free));
      while(block != stop && *getCell(puzzle, column, block, rowLen)  == '.') block+=dir;
      if (block == stop) break;

      char* freeCell = getCell(puzzle, column, free, rowLen);
      *freeCell = *getCell(puzzle, column, block, rowLen);
      *getCell(puzzle, column, block, rowLen) = '.';
    } 
//    cout << "after \n"; for (int i=0; i<len; i++) cout << *getCell(puzzle, column, i, rowLen) << endl;
}

void moveUD(char direction, int colLen, int rowLen, char* puzzle) {
    for (int i=0; i<rowLen; i++)
        moveColumnUD(direction, colLen, rowLen, puzzle, i);
} 

void printPuzzle(int turn, char* puzzle, int rows, int rowLen) {
    if (turn>-1) cout << "\n[" << turn << "]" << endl;
    for (int i=0; i<rows; i++) {
        for(int j=0; j<rowLen; j++)
            cout << *getCell(puzzle, j, i, rowLen);
        cout << endl;
    }
}

int reduceMoves(char* moves, int len, char* newMoves) {
    //cout << moves << endl;
    int idx=0;
    int i=0;
    while (i<len) {
        if((i+1) < len && moves[i] == moves[i+1]) {
            i++;
            continue;
        }
        if((i+2) < len && moves[i] == 'L' && moves[i+1] == 'P' && moves[i+2] == 'L') {
            i+=2;
            continue;
        }
        if((i+2) < len && moves[i] == 'P' && moves[i+1] == 'L' && moves[i+2] == 'P') {
            i+=2;
            continue;
        }
        if((i+2) < len && moves[i] == 'G' && moves[i+1] == 'D' && moves[i+2] == 'G') {
            i+=2;
            continue;
        }
        if((i+2) < len && moves[i] == 'D' && moves[i+1] == 'G' && moves[i+2] == 'D') {
            i+=2;
            continue;
        }
        newMoves[idx++] = moves[i++];
    }
    newMoves[idx] = 0;
    //cout << newMoves << endl;
    return idx;
}

int main(void) {
    int n,m;
    cin >> n >> m;

    char puzzle[n][m];

    for (int i=0; i<n; i++) {
        string row;
        cin >> row;
        for (int j=0; j<m; j++) {
            puzzle[i][j] = row.at(j);
        }
    }

    int movesNumber;
    cin >> movesNumber;
    char movesOrig[movesNumber+1]; 
    char moves[movesNumber+1]; 
    string inputMoves;
    cin >> inputMoves;
    for (int i=0; i<movesNumber; i++) {
         movesOrig[i] = inputMoves.at(i);
    }
    movesOrig[movesNumber]=0;
    movesNumber = reduceMoves(movesOrig, movesNumber, moves);

//    printPuzzle(0, (char*)puzzle, n, m);

    for (int k=0; k<movesNumber; k++) {
        char dir = moves[k];
        if (dir == 'L' || dir == 'P')
            moveLR(dir, n, m, (char*)puzzle);
        if (dir == 'G' || dir == 'D')
            moveUD(dir, n, m, (char*)puzzle);
//        printPuzzle(k+1, (char*)puzzle, n, m);
    }

    printPuzzle(-1, (char*)puzzle, n, m);
    return 0;
}