#K91512. Reach Target on a Grid with Obstacles

    ID: 37992 Type: Default 1000ms 256MiB

Reach Target on a Grid with Obstacles

Reach Target on a Grid with Obstacles

You are given an n x n grid, where the rows and columns are numbered from 1 to n. A token is placed at a starting cell (s_x, s_y) and must move according to a sequence of moves provided as a string. Each character in the moves string represents a unit move in one of four directions: Left (L), Right (R), Up (U), or Down (D).

The grid contains obstacles at specified coordinates. The token cannot move into a cell that is either out of bounds (i.e., outside the range \(1 \leq x, y \leq n\)) or contains an obstacle.

Your task is to determine if, after executing all the moves in the given sequence, the token will reach the target cell (t_x, t_y). If it reaches the target exactly, output "Yes"; otherwise, output "No".

Note: The moves are executed sequentially. If a move would cause the token to move out of bounds or onto an obstacle, the process stops immediately, and the answer should be "No".

inputFormat

The input is given from standard input (stdin) and consists of several lines:

  • The first line contains five integers: \(n\), \(s_x\), \(s_y\), \(t_x\), \(t_y\), where \(n\) is the grid size, and \((s_x,s_y)\) is the starting position while \((t_x,t_y)\) is the target position.
  • The second line contains a single integer \(m\) representing the number of obstacles.
  • The next \(m\) lines each contain two integers representing the coordinates of an obstacle.
  • The final line contains a string consisting of the characters 'L', 'R', 'U', 'D' which represent the sequence of moves.

It is guaranteed that the moves string, if empty, is provided as an empty line.

outputFormat

Print a single line to the standard output (stdout):

  • Output "Yes" if the token exactly reaches the target cell after all moves.
  • Output "No" otherwise.
## sample
5 1 1 5 5
2
3 3
4 4
DDRRRRDD
No

</p>