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

//#define DEBUG
//#define COMPARE
#ifdef DEBUG
#define D(x) x
#else
#define D(x)
#endif
using namespace std;

char opposite(char c) {
    return c == 'G' ? 'D' : c == 'D' ? 'G' : c == 'L' ? 'P' : 'L';
}

decltype(auto) isDir(char c) {
    static bool isTop{}, isBottom{}, isRight{}, isLeft{};
    return c == 'G' ? isTop : c == 'D' ? isBottom : c == 'L' ? isLeft : isRight;
}

string simplifyMoves(const string &moves) {
    string ret;
    for (auto c: moves) {
        if (isDir(c)) continue;
        if (size(ret) == 0 || ret.back() != opposite(c)) ret += c;
        else if (size(ret) > 2) ret.pop_back();
        else ret.back() = c;
        isDir(c) = true;
        isDir(opposite(c)) = false;
    }
    return ret;
}

template <typename S, typename T>
void applyMove(S &state, T zero, char move) {
    if (move == 'G') {
        for (int x = 0; x < size(state[0]); ++x) {
            for (int from = 0, to = 0; from < size(state); ++from) {
                if (state[from][x] != zero) {
                    state[to][x] = state[from][x];
                    if (from != to) state[from][x] = zero;
                    ++to;
                }
            }
        }
    }
    if (move == 'D') {
        for (int x = 0; x < size(state[0]); ++x) {
            for (int from = (int)size(state) - 1, to = (int)size(state) - 1; from >= 0; --from) {
                if (state[from][x] != zero) {
                    state[to][x] = state[from][x];
                    if (from != to) state[from][x] = zero;
                    --to;
                }
            }
        }
    }
    if (move == 'P') {
        for (int y = 0; y < size(state); ++y) {
            for (int from = (int)size(state[0]) - 1, to = (int)size(state[0]) - 1; from >= 0; --from) {
                if (state[y][from] != zero) {
                    state[y][to] = state[y][from];
                    if (from != to) state[y][from] = zero;
                    --to;
                }
            }
        }
    }
    if (move == 'L') {
        for (int y = 0; y < size(state); ++y) {
            for (int from = 0, to = 0; from < (int)size(state[0]); ++from) {
                if (state[y][from] != zero) {
                    state[y][to] = state[y][from];
                    if (from != to) state[y][from] = zero;
                    ++to;
                }
            }
        }
    }
}

void print_state(const vector<string> &state, ostream &out) {
    for (const auto &s : state) out << s << endl;
    out << endl;
}

void print_num_state(const vector<vector<int>> &state) {
    for (const auto &row : state) {
        for (auto elem: row)
            cerr << std::setfill(' ') << std::setw(2) << elem << ' ';
        cerr << endl;
    }
    cerr << endl;
}

vector<vector<int>> get_cycles(const vector<int> &permutation) {
    vector<bool> handled(size(permutation));
    vector<vector<int>> cycles;
    for (int i = 0; i < size(permutation); ++i) {
        if (handled[i]) continue;
        cycles.emplace_back();
        int j = i;
        while (!handled[j]) {
            cycles.back().push_back(j);
            handled[j] = true;
            j = permutation[j];
        }
    }
    return cycles;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m; cin >> n >> m;
    vector<string> state(n);
    for (auto &s : state) cin >> s;
    int k; cin >> k;
    string moves; cin >> moves;
    D(
            cerr << "input state:" << endl;
            print_state(state, cerr);
            cerr << "moves: " << moves << " (" << size(moves) << ')' << endl;
    )
#ifdef COMPARE
    auto state_copy = state;
    for (int i = 0; i < size(moves); ++i) applyMove(state_copy, '.', moves[i]);
    cout << "correct answer:" << endl;
    print_state(state_copy, cout);
#endif
    moves = simplifyMoves(moves);
    D(
            cerr << "simplified moves: " << moves << " (" << size(moves) << ')' << endl;
    )
    if (size(moves) < 2) {
        for (int i = 0; i < size(moves); ++i) applyMove(state, '.', moves[i]);
        print_state(state, cout);
#ifdef COMPARE
        cout << (state == state_copy ? "OK" : "NOK") << endl;
#endif
        return 0;
    }
    int initialMoveCount = (int)size(moves) % 4;
    if (initialMoveCount == 0) initialMoveCount = 4;
    if (initialMoveCount == 1) initialMoveCount = 5;
    for (int i = 0; i < initialMoveCount; ++i) {
        applyMove(state, '.', moves[i]);
    }
    D(
            cerr << "initial move count: " << initialMoveCount << endl;
            cerr << "state after initial moves:" << endl;
            D(print_state(state, cerr);)
    )
    int rotationsNum = ((int)size(moves) - initialMoveCount) / 4;
    D(cerr << "full rotations: " << rotationsNum << endl;)
    if (rotationsNum == 0) {
        print_state(state, cout);
#ifdef COMPARE
        cout << (state == state_copy ? "OK" : "NOK") << endl;
#endif
        return 0;
    }
    vector<vector<int>> numState(size(state));
    vector<char> numMap;
    int counter{};
    for (auto &s : numState) s.resize(size(state[0]), -1);
    for (int y = 0; y < size(state); ++y) {
        for (int x = 0; x < size(state[0]); ++x) {
            if (state[y][x] != '.') {
                numMap.push_back(state[y][x]);
                numState[y][x] = counter++;
            }
        }
    }
    D(
            cerr << "num state:" << endl;
            print_num_state(numState);
    )
    for (int i = initialMoveCount; i < initialMoveCount + 4; ++i) applyMove(numState, -1, moves[i]);
    D(
            cerr << "num state after 4 moves:" << endl;
            print_num_state(numState);
    )
    vector<int> permutation(counter);
    counter = 0;
    for (int y = 0; y < size(state); ++y) {
        for (int x = 0; x < size(state[0]); ++x) {
            if (numState[y][x] != -1) {
                permutation[counter++] = numState[y][x];
            }
        }
    }
    auto cycles = get_cycles(permutation);
    D(
            cerr << "permutation:" << endl;
            print_num_state({permutation});
            cerr << "cycles:" << endl;
            print_num_state(cycles);
    )
    for (const auto &cycle: cycles) {
        int shift = rotationsNum % (int)size(cycle);
        for (int i = 0; i < size(cycle); ++i) {
            int s = (i + shift >= size(cycle)) ? shift - (int)size(cycle) : shift;
            permutation[cycle[i]] = cycle[i + s];
        }
    }
    D(
        cerr << "permutation after " << rotationsNum << " changes:" << endl;
        print_num_state({permutation});
    )
    counter = 0;
    for (auto &row : state) {
        for (auto &elem : row) {
            if (elem != '.') {
                elem = numMap[permutation[counter++]];
            }
        }
    }
    print_state(state, cout);
#ifdef COMPARE
    cout << (state == state_copy ? "OK" : "NOK") << endl;
#endif
}