#K34087. Navigate the Game Map

    ID: 25231 Type: Default 1000ms 256MiB

Navigate the Game Map

You are given a grid-based game map where a player must navigate from a starting position 'S' to a goal 'G'. The map is represented by an N x M grid, where each cell is one of:

  • . : an empty cell
  • # : an obstacle
  • S : the start position
  • G : the goal position

The player can move in one of four directions using commands: U (up), D (down), L (left), and R (right). When processing a command, the player moves to the adjacent cell in that direction if it is within the grid and is not an obstacle. Otherwise, the move is ignored. In mathematical form, if the player's current position is \((x, y)\), then a move is valid if: $$0 \leq x' < N,\quad 0 \leq y' < M$$ where \((x', y')\) is the next position (and the corresponding grid cell is not a wall).

Your task is to determine if the player reaches the goal G after processing all the commands. Output Success if the goal is reached, otherwise Failure.

inputFormat

The input consists of multiple test cases. For each test case:

  1. A line containing two integers N and M representing the number of rows and columns respectively. The input terminates when N = 0 and M = 0.
  2. N lines follow, each containing a string of length M representing the grid.
  3. A line containing an integer C, the number of commands.
  4. A line containing a string of C characters representing the sequence of commands.

All input is read from standard input (stdin).

outputFormat

For each test case, print a single line containing either Success (if the goal is reached after all valid moves) or Failure (otherwise). All output is printed to standard output (stdout).

## sample
5 5
.....
..#.S
.....
...#.
...G.
8
RRRRDDDL
2 2
SG
..
4
DRUL
3 3
S..
...
..G
5
DDDRR
0 0
Success

Failure Success

</p>