#K34087. Navigate the Game Map
Navigate the Game Map
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 obstacleS
: the start positionG
: 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:
- A line containing two integers
N
andM
representing the number of rows and columns respectively. The input terminates whenN = 0
andM = 0
. N
lines follow, each containing a string of lengthM
representing the grid.- A line containing an integer
C
, the number of commands. - 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).
5 5
.....
..#.S
.....
...#.
...G.
8
RRRRDDDL
2 2
SG
..
4
DRUL
3 3
S..
...
..G
5
DDDRR
0 0
Success
Failure
Success
</p>