#K53807. Drone Return to the Hub

    ID: 29613 Type: Default 1000ms 256MiB

Drone Return to the Hub

Drone Return to the Hub

Given a drone starting at the origin (0, 0) on a 2D plane and a series of movement instructions, determine if the drone returns to its starting position after executing all instructions. The instructions are given as a string of characters where:

  • U represents a move upward (increase y by 1)
  • D represents a move downward (decrease y by 1)
  • L represents a move leftward (decrease x by 1)
  • R represents a move rightward (increase x by 1)

The task is to determine whether the drone ends up at the central hub (0, 0) after following all the given moves.

The movement changes can be mathematically formulated as follows:

\( x = \sum_{i=1}^{n} \Delta x_i \) and \( y = \sum_{i=1}^{n} \Delta y_i \), where \(\Delta x_i\) and \(\Delta y_i\) are changes in the x and y coordinates respectively for each instruction. The drone returns to the hub if and only if \( x = 0 \) and \( y = 0 \).

inputFormat

The input is read from standard input (stdin). The first line contains an integer ( n ), representing the number of instructions. The second line contains a string of length ( n ) consisting of the characters 'U', 'D', 'L', and 'R' only.

outputFormat

Print to standard output (stdout) "YES" if the drone returns to the hub at (0, 0) after executing all the instructions, otherwise print "NO".## sample

4
UDLR
YES