Apple

Time limit1sMemory limit128 MB

Summary
Simulate gravity, rotations, and waiting on a board with one apple, then print the final board state.
Level

Medium7 of 10

Topics
Simulation, Implementation, Matrix
Solved
No attempts yet

Problem

A game board has RR rows and SS columns. Every cell is either empty or blocked, and one cell holds an apple. The apple always falls in the direction of gravity, which is toward the bottom of the screen.

Every second, the player may rotate the screen 90 degrees clockwise, rotate it 90 degrees counterclockwise, or wait and do nothing. The board turns together with the screen, and gravity keeps pointing toward the bottom of the screen. After that, the apple falls one cell toward the bottom of the screen, but only if the cell below it exists (is inside the board) and is empty. Otherwise the apple stays where it is.

Write a simulator for this game: given the initial board and a sequence of moves, print the final board. On the board, # marks a blocked cell, . marks an empty cell, and J marks the apple.

Input

The first line contains two positive integers RR and SS (3≤R,S≤10003 \le R, S \le 1000), the number of rows and columns of the board.

Each of the next RR lines contains exactly SS characters. Each character is an uppercase J, a . (dot), or a #. The board contains exactly one J.

The last line contains the move sequence, a string of at most 1,000,000 characters. Each character is one of the following.

  • R: rotate the screen clockwise.
  • L: rotate the screen counterclockwise.
  • P: wait.

Output

Print the final board as it appears on the screen after all moves. Depending on the final rotation, it is a grid of RR rows and SS columns or a grid of SS rows and RR columns.

Examples2

  1. Example 1

    Input
    5 3
    #..
    J..
    #..
    ..#
    ...
    RPPLPL
    
    Expected output
    ...#.
    ..J..
    #.#..
    
  2. Example 2

    Input
    3 3
    ...
    .J.
    ...
    P
    
    Expected output
    ...
    ...
    .J.