#K92462. Robot Final Position Calculator
Robot Final Position Calculator
Robot Final Position Calculator
You are given a series of test cases where each test case consists of a string of characters. Each character represents a movement for a robot starting from the origin (0, 0):
- L: Move left (decrease the x-coordinate by 1).
- R: Move right (increase the x-coordinate by 1).
- U: Move up (increase the y-coordinate by 1).
- D: Move down (decrease the y-coordinate by 1).
Your task is to compute the final position of the robot after executing the series of moves for each test case. The final position should be output in the format x y
, where x
and y
are the final coordinates.
The movement calculations can be expressed mathematically as follows:
For each instruction string, let \( x_0 = 0, y_0 = 0 \). For each character \( c \) in the string, update the coordinates as:
[ x = x + \begin{cases} -1 & \text{if } c = \text{'L'} \ 1 & \text{if } c = \text{'R'} \ 0 & \text{otherwise} \end{cases}, \quad y = y + \begin{cases} 1 & \text{if } c = \text{'U'} \ -1 & \text{if } c = \text{'D'} \ 0 & \text{otherwise} \end{cases}. ]
Output the final \(x\) and \(y\) separated by a space for each test case.
inputFormat
The input is read from standard input. It consists of multiple lines:
- The first line contains an integer \(T\) representing the number of test cases.
- Each of the following \(T\) lines contains a non-empty string consisting of the characters 'L', 'R', 'U', and 'D' that represents a sequence of moves.
outputFormat
For each test case, output a single line containing two integers \(x\) and \(y\) separated by a space. These represent the final coordinates of the robot after executing the given movements.
## sample2
LLRR
UUDD
0 0
0 0
</p>