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
#include<bits/stdc++.h>
using namespace std;

#define MAX_SIZE 507
#define X first
#define Y second

int rows, cols;
int total_moves;
char fence = '@';
char blank = '.';
char board[MAX_SIZE][MAX_SIZE];

map<char, pair<int, int>> move_vectors = {
    { 'G', { 0,  1} },
    { 'D', { 0, -1} },
    { 'L', {-1,  0} },
    { 'P', { 1,  0} },
};

template <typename T,typename U>                                                   
pair<T,U> operator+(const pair<T,U> &l, const pair<T,U> &r) {   
    return make_pair(l.first + r.first, l.second + r.second);                                    
}

void print_board() {
    for (int row = rows; row > 0; row--) {
        for (int col = 1; col <= cols; col++) {
            cout << board[col][row];
        }
        cout << "\n";
    }
}

char get_board(pair<int, int> coord) {
    return board[coord.X][coord.Y];
}

void swap_board(pair<int, int> coord_1, pair<int, int> coord_2) {
    swap(board[coord_1.X][coord_1.Y], board[coord_2.X][coord_2.Y]);
}

void apply_move_one_block(pair<int, int> current_coord, pair<int, int> single_move) {
    auto next_coord = current_coord + single_move;
    while (get_board(next_coord) == blank) {
        swap_board(current_coord, next_coord);
        current_coord = next_coord;
        next_coord = next_coord + single_move;
    }
}

void apply_move(char next_move) {
    if (next_move == 'G') {
        for (int row = rows; row > 0; row--) {
            for (int col = 1; col <= cols; col++) {
                apply_move_one_block(make_pair(col, row), move_vectors[next_move]);
            }
        }
        return;
    }
    if (next_move == 'D') {
        for (int row = 1; row <= rows; row++) {
            for (int col = 1; col <= cols; col++) {
                apply_move_one_block(make_pair(col, row), move_vectors[next_move]);
            }
        }
        return;
    }
    if (next_move == 'L') {
        for (int col = 1; col <= cols; col++) {
            for (int row = 1; row <= rows; row++) {
                apply_move_one_block(make_pair(col, row), move_vectors[next_move]);
            }
        }
        return;
    }
    if (next_move == 'P') {
        for (int col = cols; col > 0; col--) {
            for (int row = 1; row <= rows; row++) {
                apply_move_one_block(make_pair(col, row), move_vectors[next_move]);
            }
        }
        return;
    }
}


int main() {

    ios_base::sync_with_stdio(false);
    cin >> rows >> cols;

    for (int i = 0; i <= rows + 1; i++) {
        board[0][i] = fence;
        board[cols + 1][i] = fence;
    }
    for (int i = 0; i <= cols + 1; i++) {
        board[i][0] = fence;
        board[i][rows + 1] = fence;
    }

    for (int row = rows; row > 0; row--) {
        for (int col = 1; col <= cols; col++) {
            cin >> board[col][row];
        }
    }

    cin >> total_moves;
    for (int move_cnt = 1; move_cnt <= total_moves; move_cnt++) {
        char next_move;
        cin >> next_move;
        apply_move(next_move);
    }

    print_board();
    return 0;
}