#K43167. Unique Robot Positions
Unique Robot Positions
Unique Robot Positions
A robot starts at the origin (0, 0) of a 2D grid. It receives a movement string D composed of the characters U
(up), D
(down), L
(left), and R
(right). For each character in the string, the robot moves one unit in the specified direction. The task is to calculate the number of unique positions that the robot visits, including the starting position.
Formally, if the robot moves according to the directions in the string:
\[ \text{Let } (x_0,y_0) = (0,0) \]\n\[ (x_i,y_i) = \text{position after the } i\text{-th move} \]
We need to compute the size of the set
\( S = \{(x_0,y_0), (x_1,y_1), \dots, (x_n,y_n)\} \)
where \( n \) is the length of the string. The answer is \(|S|\).
inputFormat
The input consists of a single line containing a string D
representing the movement directions. The string can be empty.
Each character of D
is one of 'U', 'D', 'L', or 'R'.
outputFormat
Output a single integer, which is the number of unique positions the robot visits.
## sampleURDL
4