#K61442. Robot Return to the Origin
Robot Return to the Origin
Robot Return to the Origin
This problem challenges you to determine whether a robot that follows a sequence of movement commands returns to its original position at the origin (0, 0). The commands are given as a string where each character represents a unit move in one of the four cardinal directions: L
(left), R
(right), U
(up), and D
(down).
The robot starts at (0, 0) and moves one unit per command. After executing all the commands, if the robot is back at the origin, output YES
; otherwise, output NO
.
The movement can be mathematically expressed using the coordinate system. Let the initial position be \((0,0)\). Each command modifies the position as follows:
- L: \(x = x - 1\)
- R: \(x = x + 1\)
- U: \(y = y + 1\)
- D: \(y = y - 1\)
The goal is to check if \(x=0\) and \(y=0\) after processing the entire string of commands.
inputFormat
The input consists of a single line containing a string of characters. Each character is one of L
, R
, U
, or D
, representing a movement command.
outputFormat
Output a single line with the word YES
if the robot returns to the origin after executing all commands, otherwise output NO
.
UDLR
YES