#K37367. Grid Movement Simulation

    ID: 25961 Type: Default 1000ms 256MiB

Grid Movement Simulation

Grid Movement Simulation

You are given a grid representing a game board with obstacles and a player's initial position. The grid consists of characters: P (the player's starting position), # (obstacle), and . (free space). You are also given a sequence of movements. The moves are represented by the characters U (up), D (down), L (left), and R (right).

For each move, if the player attempts to move outside of the grid or into an obstacle, the simulation fails immediately and the output should be Failure. Otherwise, if the player successfully performs all moves, output Success.

In mathematical terms, let the current position be \((x, y)\). For a move with delta \((\Delta x, \Delta y)\), the new position becomes \((x + \Delta x, y + \Delta y)\). If the new position is either out of bounds or a cell with a \(#\), the move is invalid.

inputFormat

The input is read from stdin and has the following format:

  1. The first line contains an integer \(n\) representing the number of rows in the grid.
  2. The next \(n\) lines each contain a string representing a row of the grid.
  3. The final line contains a string denoting the movements. This string may be empty.

outputFormat

Output to stdout a single string: either Success if all movements are executed without error, or Failure if an invalid move is encountered.

## sample
5
#####
#...#
#.#.#
#P..#
#####
UURRD
Success