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
#include <stdio.h>

void print(char **data, const int rows, const int columns) {
  for (int row = 0; row < rows; ++row) {
    printf("%s\n", data[row]);
  }
  printf("\n");
}

void makeMove(char **data, const int rows, const int columns, const char move) {
  char newData[501];
  int newDataCount = 0;
  if (move == 'G') {
    for (int column = 0; column < columns; ++column) {
      newDataCount = 0;
      for (int row = 0; row < rows; ++row) {
        if (data[row][column] != '.') {
          newData[newDataCount++] = data[row][column];
          data[row][column] = '.';
        }
      }
      for (int i = 0; i < newDataCount; ++i) {
        data[i][column] = newData[i];
      }
    }
  } else if (move == 'D') {
    for (int column = 0; column < columns; ++column) {
      newDataCount = 0;
      for (int row = rows - 1; 0 <= row; --row) {
        if (data[row][column] != '.') {
          newData[newDataCount++] = data[row][column];
          data[row][column] = '.';
        }
      }
      for (int i = 0; i < newDataCount; ++i) {
        data[rows - 1 - i][column] = newData[i];
      }
    }
  } else if (move == 'L') {
    for (int row = 0; row < rows; ++row) {
      newDataCount = 0;
      for (int column = 0; column < columns; ++column) {
        if (data[row][column] != '.') {
          newData[newDataCount++] = data[row][column];
          data[row][column] = '.';
        }
      }
      for (int i = 0; i < newDataCount; ++i) {
        data[row][i] = newData[i];
      }
    }
  } else if (move == 'P') {
    for (int row = 0; row < rows; ++row) {
      newDataCount = 0;
      for (int column = columns - 1; 0 <= column; --column) {
        if (data[row][column] != '.') {
          newData[newDataCount++] = data[row][column];
          data[row][column] = '.';
        }
      }
      for (int i = 0; i < newDataCount; ++i) {
        data[row][columns - 1 - i] = newData[i];
      }
    }
  }
}

int main() {
  int rows, columns;
  char **data;
  scanf("%d %d", &rows, &columns);
  data = new char *[rows];
  for (int row = 0; row < rows; ++row) {
    data[row] = new char[columns + 1];
    scanf("%s", data[row]);
  }

  int movesCount;
  char moves[500001];
  scanf("%d", &movesCount);
  scanf("%s", moves);
  for (int i = 0; i < movesCount; ++i) {
    makeMove(data, rows, columns, moves[i]);
  }
  print(data, rows, columns);
  return 0;
}