eolymp
bolt
Try our new interface for solving problems
Problems

Robot Encryption

Robot Encryption

\includegraphics{https://static.e-olymp.com/content/4f/4f6fec1cf4971805e2ac189f24bf2b32c9db3ad6.jpg} Due to suspicion of cheaters, one of the more paranoid problem setters has started encrypting all messages to the rest of the jury before sending them. He didn't use any standard encryption, however, as he believes those are all part of a giant conspiracy network trying to crush IDI Open from the inside. Instead, he based it on the fact that the cheaters are likely to be the worst programmers. The decryption requires some programming skill, and should therefore be safe. Along with the encrypted message, he sent explanation of how to decrypt it. The only problem now is that not all jury members are able to implement the decryption. This is where we need your help. You need to help us decrypt these messages by writing a program that does the task. Decryption is performed by simulating a robots movement on a grid. The robotis initially placed in the north-west corner of the grid, facing south. The robot is a simple one, and only accepts three different commands: \begin{enumerate} \item \texttt{L} turns the robot \textbf{90°} to the left \item \texttt{R} turns the robot \textbf{90°} to the right \item \texttt{F} moves the robot one square forward. If moving forward would cause the robot to fall off the grid, the robot instead makes a \textbf{180°} turn without moving. \end{enumerate} Instructions to the robot is given in a series of commandsets. A commandset is a string of commands, with the possible addition of loops. A loop is given on the form \texttt{(commandset)number} where \texttt{number} is the number of times the commandset inside the parentheses should be run. Longer sequences of commands can be built up recursively in this fashion. More formally: \begin{lstlisting} commandset ::= instruction+ instruction ::= command|loop loop ::= "(" commandset ")" number command ::= R|L|F number ::= 1|2|3|4|5|6|7|8|9 \end{lstlisting} The decrypted text is the text string obtained by concatenating the characters on the grid positions the robot stands on after executing each commandline. \InputFile The first line of input gives $T$ ($0 < T ≤ 100$), the number of test scenarios. Each scenario starts with a line containing $W$ ($0 < W ≤ 50$) and $H$ ($0 < H ≤ 50$)separated by a single space, describing the dimensions of the grid. Then follows $H$ lines, each consisting of $W$ characters, making up the grid. After this comes a line containing $N$ ($0 < N ≤ 20$) the number of commandlines, followed by the $N$ lines the robot will be executing. Commandlines will be no longer than $50$ characters, and will follow the syntax given in the problem text. No character with ASCII value lower than $32$ or higher than $126$ will appear on the robots grid. \OutputFile One line per test scenario, containing the decrypted text.
Time limit 1 second
Memory limit 64 MiB
Input example #1
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
Output example #1
HELLO WORLD!