A robot in a grid moves forward or turns according to a fixed sequence of turns; decide whether it can reach the exit once the turns run out.
Medium7BFSSimulationImplementationGraphNo attempts yetTime limit8sMemory limit512 MBA robot is placed in a two dimensional maze that has one entrance and one exit.
The maze is a grid of H rows and W columns. The top row faces north. Each cell is either empty or a wall. One empty cell is the entrance and another empty cell is the exit.
You control the robot with two buttons, forward and turn. The forward button moves the robot one cell in the direction it currently faces. The robot cannot move into a wall and cannot leave the maze, so in those cases pressing forward changes nothing. The turn button rotates the robot by 90 degrees following a fixed program of N commands. Each command is either 'L' for a left turn or 'R' for a right turn. The first press of the turn button follows the first command, the second press follows the second command, and so on. Once all N commands are used the turn button stops working, but the forward button keeps working.
The robot starts on the entrance cell facing north. You may press the two buttons in any order and as many times as you want, and the only limit is the number of turns. Decide whether the robot can reach the exit cell.
The input holds several datasets. Each dataset has this form.
H W N
s1s2...sN
c(1,1)c(1,2)...c(1,W)
...
c(H,1)c(H,2)...c(H,W)
The first line of a dataset has three integers H, W and N (1≤H,W≤1000, 1≤N≤106).
The second line has the program, a string of N characters. Each character is 'L' or 'R'.
Each of the next H lines has exactly W characters and describes one row of the maze, from the north row to the south row. '.' is an empty cell, '#' is a wall, 'S' is the entrance and 'G' is the exit. Every dataset has exactly one 'S' and exactly one 'G'.
The last line of the input is 0 0 0, which is not a dataset.
For each dataset, print Yes if the robot can reach the exit and No otherwise. Print one answer per line, in the order the datasets are given.