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
#include <cstdio>
#include <cstdlib>
#include <vector>

using std::pair;
using std::vector;

enum Move { UP, DOWN, LEFT, RIGHT };
enum Mode { INIT, VERTICAL, HORIZONTAL, CORNER };
enum Corner { UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT };
enum Rotation { CW, CCW, NONE };

bool is_vertical(Move move) { return move == UP || move == DOWN; }

const int COLOR_MASK = 0xFF;
const int ROW_MASK = 0x7FF00;
const int MARK = 0x80000;
const int COL_MASK = 0x7FF00000;
const int POS_MASK = ROW_MASK | COL_MASK;
const int ROW_SHIFT = 8;
const int COL_SHIFT = 20;

struct Board {
  int rows, cols;
  int **data;

  void load() {
    char buf[505];
    scanf("%d%d", &rows, &cols);
    data = new int *[rows];
    for (int r = 0; r < rows; ++r) {
      data[r] = new int[cols];
      scanf("%s", buf);
      for (int i = 0; i < cols; ++i) {
        data[r][i] = buf[i];
      }
    }
  }

  void print() {
    char buf[505];
    buf[cols] = '\0';
    for (int r = 0; r < rows; ++r) {
      for (int c = 0; c < cols; ++c) {
        buf[c] = data[r][c] & COLOR_MASK;
      }
      puts(buf);
    }
  }

  char color(int r, int c) { return data[r][c] & COLOR_MASK; }

  void swap(int r1, int c1, int r2, int c2) {
    int tmp = data[r1][c1];
    data[r1][c1] = data[r2][c2];
    data[r2][c2] = tmp;
  }

  void index() {
    for (int r = 0; r < rows; ++r) {
      int rrr = r << ROW_SHIFT;
      for (int c = 0; c < cols; ++c) {
        int ccc = c << COL_SHIFT;
        data[r][c] = data[r][c] | rrr | ccc;
      }
    }
  }

  inline int read(int position) { return data[(position & ROW_MASK) >> ROW_SHIFT][(position & COL_MASK) >> COL_SHIFT]; }

  inline void mark(int position) { data[(position & ROW_MASK) >> ROW_SHIFT][(position & COL_MASK) >> COL_SHIFT] |= MARK; }

  inline void set_color(int position, char color) {
    int r = (position & ROW_MASK) >> ROW_SHIFT;
    int c = (position & COL_MASK) >> COL_SHIFT;
    data[r][c] &= ~COLOR_MASK;
    data[r][c] |= color;
  }

  inline bool is_marked(int position) { return (read(position) & MARK) != 0; }

  inline bool is_marked(int r, int c) { return (data[r][c] & MARK) != 0; }

  int next_position(int position) { return read(position) & POS_MASK; }

  void advance_cycle(int r, int c, int advance) {
    int init_pos = (r << ROW_SHIFT) | (c << COL_SHIFT);
    mark(init_pos);
    vector<int> positions;
    vector<char> colors;
    positions.push_back(init_pos);
    colors.push_back((char)(read(init_pos) & COLOR_MASK));
    for (int next = next_position(init_pos); !is_marked(next); next = next_position(next)) {
      positions.push_back(next);
      colors.push_back((char)(read(next) & COLOR_MASK));
      mark(next);
    }
    // for (int v : positions) {
    //   printf("%c", (char)(read(v) & COLOR_MASK));
    // }

    // printf("\n");

    int len = positions.size();
    if (len > 1) {
      for (int i = 0; i < len; ++i) {
        set_color(positions[i], colors[(i + advance) % len]);
      }
    }
  }

  void find_and_advance_cycles(int advance) {
    for (int r = 0; r < rows; ++r) {
      for (int c = 0; c < cols; ++c) {
        if (color(r, c) != '.' && !is_marked(r, c)) {
          advance_cycle(r, c, advance);
        }
      }
    }
  }

  void simulate_move(Move move) {
    if (is_vertical(move)) {
      for (int c = 0; c < cols; ++c) {
        int add = (move == UP) ? 1 : -1;
        int begin = (move == UP) ? 0 : rows - 1;
        int end = (move == UP) ? rows : -1;
        for (int put = begin, seek = begin; seek != end && put != end; put += add, seek += add) {
          while (put != end && color(put, c) != '.') {
            put += add;
          }
          if (put == end) {
            break;
          }
          seek = put + add;
          while (seek != end && color(seek, c) == '.') {
            seek += add;
          }
          if (seek != end) {
            swap(put, c, seek, c);
          } else {
            break;
          }
        }
      }
    } else {
      for (int r = 0; r < rows; ++r) {
        int add = (move == LEFT) ? 1 : -1;
        int begin = (move == LEFT) ? 0 : cols - 1;
        int end = (move == LEFT) ? cols : -1;
        for (int put = begin, seek = begin; seek != end && put != end; put += add, seek += add) {
          while (put != end && color(r, put) != '.') {
            put += add;
          }
          if (put == end) {
            break;
          }
          seek = put + add;
          while (seek != end && color(r, seek) == '.') {
            seek += add;
          }
          if (seek != end) {
            swap(r, put, r, seek);
          } else {
            break;
          }
        }
      }
    }
  }
};

struct Analysis {
  vector<Move> init_moves;
  Rotation rotation;
  int steps;
};

Move from_char(char c) {
  if (c == 'G')
    return UP;
  if (c == 'D')
    return DOWN;
  if (c == 'L')
    return LEFT;
  return RIGHT;
}

Move rotate(Move move, Rotation rot) {
  if (rot == NONE) {
    return move;
  } else if (rot == CW) {
    if (move == UP)
      return RIGHT;
    if (move == RIGHT)
      return DOWN;
    if (move == DOWN)
      return LEFT;
    return UP;
  } else {
    if (move == UP)
      return LEFT;
    if (move == LEFT)
      return DOWN;
    if (move == DOWN)
      return RIGHT;
    return UP;
  }
}

Corner from_moves(vector<Move> moves) {
  if (moves[0] == UP || moves[1] == UP) {
    if (moves[0] == LEFT || moves[1] == LEFT) {
      return UP_LEFT;
    } else {
      return UP_RIGHT;
    }
  } else if (moves[0] == LEFT || moves[1] == LEFT) {
    return DOWN_LEFT;
  } else {
    return DOWN_RIGHT;
  }
}

Move move_from_corner(Corner corner, Rotation rot) {
  if (corner == UP_LEFT)
    return rot == CW ? RIGHT : DOWN;
  if (corner == UP_RIGHT)
    return rot == CW ? DOWN : LEFT;
  if (corner == DOWN_RIGHT)
    return rot == CW ? LEFT : UP;
  else
    return rot == CW ? UP : RIGHT;
}

pair<Rotation, Corner> next_step(Corner corner, Move move) {
  if (corner == UP_LEFT) {
    if (move == RIGHT) {
      return {CW, UP_RIGHT};
    } else if (move == DOWN) {
      return {CCW, DOWN_LEFT};
    }
  } else if (corner == UP_RIGHT) {
    if (move == DOWN) {
      return {CW, DOWN_RIGHT};
    } else if (move == LEFT) {
      return {CCW, UP_LEFT};
    }
  } else if (corner == DOWN_RIGHT) {
    if (move == LEFT) {
      return {CW, DOWN_LEFT};
    } else if (move == UP) {
      return {CCW, UP_RIGHT};
    }
  } else if (corner == DOWN_LEFT) {
    if (move == UP) {
      return {CW, UP_LEFT};
    } else if (move == RIGHT) {
      return {CCW, DOWN_RIGHT};
    }
  }
  return {NONE, corner};
}

Analysis analyse(char *moves) {
  vector<Move> init_moves;
  Mode mode = INIT, next_mode = INIT;
  Move first_significant;
  Corner corner;
  int cw_rotations = 0;

  for (char *it = moves; *it; ++it, mode = next_mode) {
    Move move = from_char(*it);
    if (mode == INIT) {
      first_significant = move;
      next_mode = is_vertical(move) ? VERTICAL : HORIZONTAL;
    } else if (mode == VERTICAL) {
      if (is_vertical(move)) {
        first_significant = move;
      } else {
        init_moves.push_back(first_significant);
        init_moves.push_back(move);
        corner = from_moves(init_moves);
        next_mode = CORNER;
      }
    } else if (mode == HORIZONTAL) {
      if (!is_vertical(move)) {
        first_significant = move;
      } else {
        init_moves.push_back(first_significant);
        init_moves.push_back(move);
        corner = from_moves(init_moves);
        next_mode = CORNER;
      }
    } else /* CORNER */ {
      auto step = next_step(corner, move);
      if (step.first == CW) {
        cw_rotations++;
      } else if (step.first == CCW) {
        cw_rotations--;
      }
      corner = step.second;
    }
  }
  if (mode == CORNER) {
    Rotation rot = cw_rotations > 0 ? CW : (cw_rotations < 0 ? CCW : NONE);
    int steps = rot == CW ? cw_rotations : -cw_rotations;
    return {.init_moves = init_moves, .rotation = rot, .steps = steps};
  } else {
    init_moves.push_back(first_significant);
    return {.init_moves = init_moves, .rotation = NONE, .steps = 0};
  }
}

void simulate(Analysis &analysis, Board &board) {
  for (Move m : analysis.init_moves) {
    board.simulate_move(m);
  }
  Corner corner = from_moves(analysis.init_moves);
  int full_rotate = 4;
  int all_steps = analysis.steps;
  int rest = analysis.steps % full_rotate;
  all_steps -= rest;
  Move rotated = move_from_corner(corner, analysis.rotation);
  for (int i = 0; i < rest; ++i) {
    board.simulate_move(rotated);
    rotated = rotate(rotated, analysis.rotation);
  }
  // printf("Ref Print: \n");
  // board.print();
  board.index();
  if (all_steps > 0) {
    for (int i = 0; i < full_rotate; ++i) {
      board.simulate_move(rotated);
      rotated = rotate(rotated, analysis.rotation);
    }
  }
  // printf("\nRotated Print: \n");
  // board.print();
  int to_advance = (all_steps - full_rotate) / 4;
  if (to_advance > 0) {
    board.find_and_advance_cycles(to_advance);
  }
}

int main() {
  Board board;
  board.load();
  char command[500500];
  scanf("%*d%s", command);
  Analysis analysis = analyse(command);
  simulate(analysis, board);
  // printf("\nFinal Print: \n");
  board.print();
  return 0;
}