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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include <bits/stdc++.h>

using namespace std;

enum class Move
{
  Up,
  Right,
  Down,
  Left,
};

Move opposite(Move m)
{
  switch (m)
  {
    case Move::Up:
      return Move::Down;
    case Move::Down:
      return Move::Up;
    case Move::Right:
      return Move::Left;
    case Move::Left:
      return Move::Right;
    default:
      throw "invalid move";
  }
}

Move move_from_char(char c)
{
  switch (c)
  {
    case 'G':
      return Move::Up;
    case 'D':
      return Move::Down;
    case 'P':
      return Move::Right;
    case 'L':
      return Move::Left;
    default:
      throw "invalid move";
  }
}

char move_to_char(Move m)
{
  switch (m)
  {
    case Move::Up:
      return 'G';
    case Move::Down:
      return 'D';
    case Move::Right:
      return 'P';
    case Move::Left:
      return 'L';
    default:
      throw "invalid move";
  }
}

vector<Move> moves_from_string(const string& s)
{
  vector<Move> moves;
  for (auto c : s) moves.push_back(move_from_char(c));
  return moves;
}

vector<Move> optimized(const vector<Move>& moves)
{
  vector<Move> result;
  for (auto m : moves)
  {
    if (!result.empty())
      if (m == opposite(result.back()) || m == result.back()) result.pop_back();

    if (result.size() < 2 || result[result.size() - 2] != m)
      result.push_back(m);
  }
  return result;
}

enum class Field
{
  Empty,
  White,
  Black,
};

Field field_from_char(char c)
{
  switch (c)
  {
    case '.':
      return Field::Empty;
    case 'B':
      return Field::White;
    case 'C':
      return Field::Black;
    default:
      throw "invalid field";
  }
}

char field_to_char(Field f)
{
  switch (f)
  {
    case Field::Empty:
      return '.';
    case Field::White:
      return 'B';
    case Field::Black:
      return 'C';
    default:
      throw "invalid field";
  }
}

vector<Field> fields_from_string(const string& s)
{
  vector<Field> fields;
  for (auto c : s) fields.push_back(field_from_char(c));
  return fields;
}

struct TestCase
{
  size_t height, width;
  vector<vector<Field>> fields;
  vector<Move> moves;
};

TestCase read_test_case();
void solve_test_case(const TestCase&);

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve_test_case(read_test_case());
}

TestCase read_test_case()
{
  TestCase tc;
  cin >> tc.height >> tc.width;

  for (size_t _ = 0; _ < tc.height; _++)
  {
    string row;
    cin >> row;
    tc.fields.push_back(fields_from_string(row));
  }

  size_t move_count;
  cin >> move_count;

  string moves;
  cin >> moves;

  tc.moves = moves_from_string(moves);
  return tc;
}

struct Block
{
  Field field;
  size_t id;
};

using Blocks = vector<vector<Block>>;
Blocks number_blocks(const vector<vector<Field>>& fields);

void print_blocks(const Blocks& blocks)
{
  for (const auto& row : blocks)
  {
    for (const auto& block : row) cout << field_to_char(block.field);
    cout << "\n";
  }
}

Blocks& move_up(Blocks& blocks);
Blocks& move_left(Blocks& blocks);
Blocks& move_right(Blocks& blocks);
Blocks& move_down(Blocks& blocks);

Blocks& apply_move(Blocks& blocks, Move m)
{
  switch (m)
  {
    case Move::Up:
      return move_up(blocks);
    case Move::Down:
      return move_down(blocks);
    case Move::Left:
      return move_left(blocks);
    case Move::Right:
      return move_right(blocks);
    default:
      throw "invalid move";
  }
}

void solve_small(Blocks blocks, const vector<Move>& moves)
{
  for (auto m : moves) apply_move(blocks, m);
  print_blocks(blocks);
}

void solve_big(Blocks blocks, const vector<Move>& moves);

void solve_test_case(const TestCase& tc)
{
  auto moves = optimized(tc.moves);
  auto blocks = number_blocks(tc.fields);

  if (moves.size() < 12)
    solve_small(move(blocks), move(moves));
  else
    solve_big(move(blocks), move(moves));
}

Blocks number_blocks(const vector<vector<Field>>& fields)
{
  Blocks result(fields.size(), vector<Block>(fields[0].size()));

  size_t block_count = 0;
  for (size_t row = 0; row < fields.size(); row++)
    for (size_t column = 0; column < fields[row].size(); column++)
    {
      result[row][column].field = fields[row][column];
      if (fields[row][column] != Field::Empty)
      {
        result[row][column].id = block_count;
        block_count++;
      }
    }

  return result;
}

Blocks& move_up(Blocks& blocks)
{
  for (size_t column = 0; column < blocks[0].size(); column++)
  {
    size_t free = 0;
    for (size_t row = 0; row < blocks.size(); row++)
      if (blocks[row][column].field != Field::Empty)
      {
        swap(blocks[row][column], blocks[free][column]);
        free++;
      }
  }
  return blocks;
}

Blocks& move_down(Blocks& blocks)
{
  for (size_t column = 0; column < blocks[0].size(); column++)
  {
    size_t free = blocks.size() - 1;
    for (int row = blocks.size() - 1; row >= 0; row--)
      if (blocks[row][column].field != Field::Empty)
      {
        swap(blocks[row][column], blocks[free][column]);
        free--;
      }
  }
  return blocks;
}

Blocks& move_left(Blocks& blocks)
{
  for (size_t row = 0; row < blocks.size(); row++)
  {
    size_t free = 0;
    for (size_t column = 0; column < blocks[0].size(); column++)
      if (blocks[row][column].field != Field::Empty)
      {
        swap(blocks[row][column], blocks[row][free]);
        free++;
      }
  }
  return blocks;
}

Blocks& move_right(Blocks& blocks)
{
  for (size_t row = 0; row < blocks.size(); row++)
  {
    size_t free = blocks[0].size() - 1;
    for (int column = blocks[0].size() - 1; column >= 0; column--)
      if (blocks[row][column].field != Field::Empty)
      {
        swap(blocks[row][column], blocks[row][free]);
        free--;
      }
  }
  return blocks;
}

vector<size_t> get_permutation(const Blocks& before, const Blocks& after);
vector<size_t> compose(const vector<size_t>& perm, size_t repeat);
vector<size_t> compose(
    const vector<size_t>& first, const vector<size_t>& second);

Blocks& apply_permutation(Blocks& blocks, const vector<size_t>& perm);

// At least 12 moves
void solve_big(Blocks blocks, const vector<Move>& moves)
{
  size_t skip = 4 + moves.size() % 4;
  for (size_t i = 0; i < skip; i++) apply_move(blocks, moves[i]);

  const Blocks start = blocks;
  for (size_t i = skip; i < skip + 4; i++) apply_move(blocks, moves[i]);
  const Blocks step = blocks;

  const size_t repeat = (moves.size() - skip - 4) / 4;

  auto perm = get_permutation(start, step);
  auto composed = compose(perm, repeat);

  apply_permutation(blocks, composed);
  print_blocks(blocks);
}

vector<size_t> get_permutation(const Blocks& before, const Blocks& after)
{
  vector<size_t> perm;
  for (size_t row = 0; row < before.size(); row++)
  {
    for (size_t col = 0; col < before[0].size(); col++)
    {
      if (after[row][col].field == Field::Empty) continue;
      auto position_before = before[row][col];
      auto block_after = after[row][col];

      if (block_after.id >= perm.size()) perm.resize(block_after.id + 1);
      perm[block_after.id] = position_before.id;
    }
  }

  return perm;
}

vector<size_t> compose(const vector<size_t>& perm, size_t repeat)
{
  if (repeat == 1) return perm;
  if (repeat == 2) return compose(perm, perm);

  auto half = compose(perm, repeat / 2);
  auto result = compose(half, half);
  if (repeat % 2 == 1) result = compose(result, perm);
  return result;
}

vector<size_t> compose(
    const vector<size_t>& first, const vector<size_t>& second)
{
  vector<size_t> result(first.size());
  for (size_t i = 0; i < first.size(); i++) result[i] = second[first[i]];
  return result;
}

Blocks& apply_permutation(Blocks& blocks, const vector<size_t>& perm)
{
  vector<pair<size_t, size_t>> positions;
  vector<Field> fields;

  for (size_t row = 0; row < blocks.size(); row++)
    for (size_t col = 0; col < blocks[0].size(); col++)
      if (blocks[row][col].field != Field::Empty)
      {
        auto id = blocks[row][col].id;
        if (id >= positions.size())
        {
          positions.resize(id + 1);
          fields.resize(id + 1);
        }
        positions[id] = {row, col};
        fields[id] = blocks[row][col].field;
      }

  for (size_t i = 0; i < perm.size(); i++)
  {
    auto [row, column] = positions[perm[i]];
    auto field = fields[i];

    blocks[row][column].id = i;
    blocks[row][column].field = field;
  }

  return blocks;
}