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
a = list(map(int, input().strip().split()))

n = a[0]
m = a[1]

numberOfColored = 0
currentNumberOfColored = 0


board = []

for i in range(0, n):
    stringInput = input()
    board.append(stringInput)

# print(board)

numOfMoves = int(input())

moves = input()

boardArray = [[0 for x in range(m)] for y in range(n)]

i = 0
for line in board:
    j = 0
    for character in line:
        if character == ".":
            boardArray[i][j] = 0
        elif character == "B":
            boardArray[i][j] = 1
            numberOfColored += 1
        else:
            boardArray[i][j] = 2
            numberOfColored += 1

        j += 1
    i += 1

# print(boardArray)

moveCount = 0

currentYMin = 0
currentYMax = n
currentXMin = 0
currentXMax = m

height = 0
width = 0

previousMove = ""

for move in moves:
    if move == "G" and previousMove!="G":
        currentNumberOfColored = 0
        columnFills = [0] * m
        for i in range(currentYMin, currentYMax):
            for j in range(currentXMin, currentXMax):
                if boardArray[i][j] != 0:
                    tmpValue = boardArray[i][j]
                    boardArray[i][j] = 0
                    boardArray[columnFills[j]][j] = tmpValue
                    columnFills[j] += 1
                    currentNumberOfColored += 1
                    if currentNumberOfColored == numberOfColored:
                        break
            if currentNumberOfColored == numberOfColored:
                break

        '''currentYMin = 0
        if height == 0:
            height = max(columnFills)
            currentYMax = height
        else:
            currentYMax = height'''

    elif move == "D" and previousMove!="D":
        currentNumberOfColored = 0
        columnFills = [n - 1] * m
        for i in range(currentYMax-1, currentYMin-1, -1):
            for j in range(currentXMin, currentXMax):
                if boardArray[i][j] != 0:
                    tmpValue = boardArray[i][j]
                    boardArray[i][j] = 0
                    boardArray[columnFills[j]][j] = tmpValue
                    columnFills[j] -= 1
                    currentNumberOfColored += 1
                    if currentNumberOfColored == numberOfColored:
                        break
            if currentNumberOfColored == numberOfColored:
                break

        '''if height == 0:
            height = max(columnFills)
            currentYMin = n-height
        else:
            currentYMin = n-height

        currentYMax = n'''

    elif move == "L" and previousMove!="L":
        currentNumberOfColored = 0
        rowFills = [0] * n
        for j in range(currentXMin, currentXMax):
            for i in range(currentYMin, currentYMax):
                if boardArray[i][j] != 0:
                    tmpValue = boardArray[i][j]
                    boardArray[i][j] = 0
                    boardArray[i][rowFills[i]] = tmpValue
                    rowFills[i] += 1
                    currentNumberOfColored += 1
                    if currentNumberOfColored == numberOfColored:
                        break
            if currentNumberOfColored == numberOfColored:
                break

        '''currentXMin = 0
        if width == 0:
            width = max(rowFills)
            currentXMax = width
        else:
            currentXMax = width'''

    elif move == "P" and previousMove!="P":
        currentNumberOfColored = 0
        rowFills = [m - 1] * n
        for j in range(currentXMax-1, currentXMin-1, -1):
            for i in range(currentYMin, currentYMax):
                if boardArray[i][j] != 0:
                    tmpValue = boardArray[i][j]
                    boardArray[i][j] = 0
                    boardArray[i][rowFills[i]] = tmpValue
                    rowFills[i] -= 1
                    currentNumberOfColored += 1
                    if currentNumberOfColored == numberOfColored:
                        break
            if currentNumberOfColored == numberOfColored:
                break

        '''if width == 0:
            width = max(rowFills)
            currentYMin = m - width
        else:
            currentYMin = m - width

        currentXMax = m'''

    moveCount += 1
    previousMove = move

for i in range(0, n):
    for j in range(0, m):
        if boardArray[i][j] == 0:
            board[i] = board[i][:j] + "." + board[i][j + 1:]
        elif boardArray[i][j] == 1:
            board[i] = board[i][:j] + "B" + board[i][j + 1:]
        else:
            board[i] = board[i][:j] + "C" + board[i][j + 1:]

# print(board)

for line in board:
    print(line)