Under construction

Still being built. Things may change

Tilt tile puzzle

Time limit1sMemory limit1024 MB

Summary
Simulate k tilt moves on an n by m grid of empty, white, and black tiles, then print the final grid.
Level

Medium6 of 10

Topics
Simulation, Implementation
Solved
No attempts yet

Problem

Bajtazar is playing with a puzzle shaped like a rectangle of n×mn \times m cells. Each cell is either empty or holds a white or black tile.

In each move, the puzzle is tilted in one of four directions parallel to the sides of the rectangle. All tiles then slide as far as they can in that direction, without leaving the frame and without overlapping each other.

Bajtazar made a certain sequence of moves. Print the state of the puzzle after his last move.

Input

The first line contains the height nn and the width mm (1≤n,m≤5001 \le n, m \le 500). The next nn lines describe the initial state, row by row from top to bottom. Each line has mm symbols from left to right. Each symbol is '.' for an empty cell, 'B' for a white tile, or 'C' for a black tile.

The next line contains the number kk (1≤k≤5000001 \le k \le 500000), the number of moves Bajtazar made. The last line is a string of length kk describing those moves. The letters are 'G' for up, 'D' for down, 'L' for left, and 'P' for right.

Output

Print the final state of the puzzle in the same format as the initial state: nn lines, each containing mm symbols '.', 'B', or 'C'.

Hint

The first move 'G' slides all tiles up, and the state becomes:

.BCC.
...B.
.....
.....

After the second move 'L', the tiles slide left:

BCC..
B....
.....
.....

After the third move 'P', the tiles slide right, which gives the final state.

Examples1

  1. Example 1

    Input
    4 5
    .....
    .B.C.
    ..C..
    ...B.
    3
    GLP
    
    Expected output
    ..BCC
    ....B
    .....
    .....