This page is still under construction.

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

Robotic Encryption

Time limit1sMemory limit256 MB

Summary
Simulate a robot that runs nested-loop command lines on a character grid and read the square it stops on after each line to decode the message.
Level

Medium4 of 10

Topics
Simulation, Stack
Solved
No attempts yet

Problem

A problem setter who suspects cheaters encrypts every message before sending it to the rest of the jury. He does not use a standard cipher, because he believes the standard ones are all part of one conspiracy. He assumes instead that a cheater is a weak programmer, so he built a scheme whose decryption needs a program.

He sent the decryption rules along with the encrypted message. Not every member of the jury can implement them, so write the program that does it.

Decryption simulates a robot moving on a grid. The robot starts in the north-west corner, facing south. North is the top row and west is the leftmost column, so a step to the south increases the row number by 1. The robot accepts three commands.

  • L turns the robot 90 degrees to the left.
  • R turns the robot 90 degrees to the right.
  • F moves the robot one square forward. If moving forward would take the robot off the grid, the robot turns 180 degrees on the spot and does not move.

Commands arrive one commandline at a time. A commandline is a commandset, and a commandset is a string of commands that may contain loops. A loop is written as (commandset)number, where number is how many times the commandset inside the parentheses runs. Loops nest, so a long sequence of commands is built up recursively.

commandset  ::= instruction+
instruction ::= command | loop
loop        ::= "(" commandset ")" number
command     ::= R | L | F
number      ::= 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

The robot runs the commandlines in the order given and keeps its position and its facing from one commandline to the next. Every time a commandline finishes, read the character on the square the robot stands on. Concatenating those characters, one per commandline, gives the decrypted text.

Input

The first line holds TT, the number of test scenarios. Each scenario starts with a line holding the width WW and the height HH of the grid, separated by one space. Then come HH lines of WW characters each, which make up the grid. After the grid comes a line holding NN, the number of commandlines, followed by the NN commandlines the robot runs.

  • 0<T≤1000 < T \le 100
  • 0<W≤500 < W \le 50
  • 0<H≤500 < H \le 50
  • 0<N≤200 < N \le 20
  • A commandline is at most 50 characters long and follows the grammar above.
  • Every character on the grid has an ASCII value between 32 and 126, so a grid square can hold a space.

Output

For each test scenario print one line, the decrypted text.

Examples3

  1. Example 1

    Input
    1
    6 7
    012345
    6789AB
    CDEFGH
    IJKLMN
    OPQRST
    UVWXYZ
    _! .,&
    12
    FFL(F)5
    (F)4
    (LF)2
    (L(R)6L)9
    RFRFFF
    (L(F)2)2
    LF
    FLFF
    FFFF
    LF
    FLFF
    L(F)4
    
    Expected output
    HELLO WORLD!
    
  2. Example 2

    Input
    1
    1 1
    Q
    4
    F
    (F)9
    (RFL)3
    LLRR
    
    Expected output
    QQQQ
    
  3. Example 3

    Input
    1
    5 1
    ABCDE
    6
    F
    RF
    (F)2
    (RF)4
    LLF
    (L(F)3)2
    
    Expected output
    ABDEEB