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
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>


using namespace std;

#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define x first
#define y second

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;

enum DIR{
    UP,
    DOWN,
    LEFT,
    RIGHT,
    NONE
};

void move_up(vector<vector<char>> (&t)){
    
    vi pos(t[0].size());
    
    for(int i=0; i<t.size(); i++){
        for(int j=0; j<t[i].size(); j++){
            if(t[i][j]!='.'){
                if(pos[j] != i){
                    t[pos[j]][j] = t[i][j];
                    t[i][j] = '.'; 
                }
                pos[j]++;
            }
        }
    }
}

void move_down(vector<vector<char>> (&t)){
    
    vi pos(t[0].size());
    
    for(int i=0; i<t[0].size(); i++){
        pos[i] = t.size()-1;
    }
    
    for(int i=t.size()-1; i>=0; i--){
        for(int j=0; j<t[i].size(); j++){
            if(t[i][j]!='.'){
                if(pos[j] != i){
                    t[pos[j]][j] = t[i][j];
                    t[i][j] = '.'; 
                }
                pos[j]--;
            }
        }
    }
}

void move_left(vector<vector<char>> (&t)){
    
    vi pos(t.size());
    
    for(int i=0; i<t.size(); i++){
        for(int j=0; j<t[i].size(); j++){
            if(t[i][j]!='.'){
                if(pos[i] != j){
                    t[i][pos[i]] = t[i][j];
                    t[i][j] = '.'; 
                }
                pos[i]++;
            }
        }
    }
}

void move_right(vector<vector<char>> (&t)){
    
    vi pos(t.size());
    
    for(int i=0; i<t.size(); i++){
        pos[i] = t[0].size()-1;
    }
    
    for(int i=0; i<t.size(); i++){
        for(int j=t[i].size()-1; j>=0; j--){
            if(t[i][j]!='.'){
                if(pos[i] != j){
                    t[i][pos[i]] = t[i][j];
                    t[i][j] = '.'; 
                }
                pos[i]--;
            }
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n,m;
    cin >> n >> m;
    
    vector<vector<char> > g(n);
    
    for(int i=0; i<n; i++){
        vector<char> row(m);
        for(int j=0; j<m; j++){
            char x;
            cin >> x;
            row[j] = x;
        }
        g[i] = row;
    }
    
    int k;
    cin >> k;
    
    vector<DIR> tasks;
    
    for(int i=0; i<k; i++){
        char ki;
        cin >> ki;
        
        switch(ki){
            case 'G':
                tasks.pb(UP);
                move_up(g);
                break;
            case 'D':
                tasks.pb(DOWN);
                move_down(g);
                break;
            case 'L':
                tasks.pb(LEFT);
                move_left(g);
                break;
            case 'P':
                move_right(g);
                tasks.pb(RIGHT);
                break;
            default:
                tasks.pb(NONE);
                break;
        }
    }
    
    DIR last = NONE;
    DIR lr = NONE;
    DIR ud = NONE;
    
    vector<DIR> ftasks;
    
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            cout << g[i][j];
        }
        cout << "\n";
    }
    
    return 0;
}