#C7669. Manhattan Distance Calculation
Manhattan Distance Calculation
Manhattan Distance Calculation
You are given a number of test cases. For each test case, you are given a string consisting of the characters U
, D
, L
, and R
which represent moves up, down, left, and right respectively. Starting from the origin (0,0)
, these moves displace a token on the 2D grid. Your task is to compute the Manhattan distance between the starting point and the ending point.
The Manhattan distance is defined as:
$d = |x| + |y|$
where x
and y
are the net displacements in the horizontal and vertical directions.
Example:
Input: 1 UUURRR</p>Output: 6
inputFormat
The input is read from standard input and has the following structure:
- The first line contains an integer
T
denoting the number of test cases. - Each of the next
T
lines contains a stringS
that represents a sequence of moves.
outputFormat
For each test case, output the Manhattan distance from the origin after performing the moves in that test case. Each result should be printed on a separate line to standard output.
## sample1
UDLR
0
</p>