This page is still under construction.

Parts of this page are still being built. What you see may change.

Blindfold

Time limit1sMemory limit128 MB

Summary
Given a grid with obstacles and a fixed sequence of forward/turn moves, mark every walkable square that can be a final position for some unknown start and heading.
Level

Medium6 of 10

Topics
Simulation, Bit manipulation, Matrix, Implementation
Solved
No attempts yet

Problem

Rose and Colin are playing a game in their backyard. Because the backyard is rectangular, we can view it as a grid with rr rows and cc columns. Some squares contain obstacles.

The game works as follows. Colin covers his eyes with a blindfold, and Rose carries him to some square of the backyard, setting him down so that he faces north, south, east, or west. Colin does not know this initial position or direction. Rose then tells Colin to make a sequence of mm moves, where each move is one of:

  • F — move forward one square in the direction he is facing.
  • L — turn 90 degrees counter-clockwise, staying on the same square.
  • R — turn 90 degrees clockwise, staying on the same square.

After making all of these moves, Colin ends up at some final position. Determine every square that could be his final position. You may assume that Colin's initial position, final position, and all intermediate positions always lie inside the backyard and never on a square that contains an obstacle, and that Colin always faces a direction parallel to the sides of the backyard (north, south, east, or west).

Input

The first line contains rr and the second line contains cc (1≤r≤375, 1≤c≤80)(1 \le r \le 375,\ 1 \le c \le 80). The next rr lines describe the backyard, each with cc characters: a . denotes a square Colin may walk through, and an X denotes a square with an obstacle. The next line contains the number of moves mm (0≤m≤30000)(0 \le m \le 30000), followed by mm lines describing Colin's moves. Each line holds a single character: F, L, or R.

Output

Print the backyard grid as rr lines of cc characters. Mark each obstacle square with X, each square that could be Colin's final position with *, and every other walkable square with ..

Examples1

  1. Example 1

    Input
    2
    4
    ....
    .XX.
    3
    F
    R
    F
    
    Expected output
    .*..
    .XX*