#K48032. Calculate Final Robot Position
Calculate Final Robot Position
Calculate Final Robot Position
You are given a robot that starts at the origin \((0, 0)\) on a 2D plane. The robot is given a series of movement commands, where each command consists of a number of steps and a direction.
The directions are represented by characters:
- U: Move up (increase \(y\))
- D: Move down (decrease \(y\))
- L: Move left (decrease \(x\))
- R: Move right (increase \(x\))
Your task is to determine the final coordinates \((x, y)\) of the robot after executing all the commands.
The movement can be described by the following formulas:
- If the direction is \(U\), then \(y = y + \text{steps}\).
- If the direction is \(D\), then \(y = y - \text{steps}\).
- If the direction is \(L\), then \(x = x - \text{steps}\).
- If the direction is \(R\), then \(x = x + \text{steps}\).
Note that the robot always starts from \((0, 0)\) and there is no boundary on the plane.
inputFormat
The first line of the input contains an integer \(m\) which represents the number of movement commands. The following \(m\) lines each contain a movement command in the format:
steps direction
Here, steps is an integer denoting the number of steps to move and direction is a single character among \(U\), \(D\), \(L\), \(R\).
outputFormat
Output the final coordinates of the robot in the format:
x y
where x and y are the final coordinates after executing all commands.
## sample4
10 U
15 R
5 D
20 L
-5 5