RobotSimulation
TCO10 Qual 1 · 2010-04-11 · by ivan_metelsky
Problem Statement
You are going to make the robot move according to the following algorithm. Create a String S by concatenating times copies of the given
After you have gone through all the characters of S, determine which squares have been visited by the robot. A square is considered visited if the robot has been in it at least once. The starting and ending squares are considered visited. Return the total number of visited squares.
Constraints
- program will contain between 1 and 10 characters, inclusive.
- Each character in program will be one of 'U', 'D', 'L' or 'R'.
- times will be between 1 and 200,000,000, inclusive.
"RRR" 100 Returns: 301
The robot will move to the right 300 times, so 301 squares will be visited.
"DDU" 100 Returns: 102
The robot will move according to the pattern "DDU" 100 times. Initially, we have 1 visited square. The first time the pattern "DDU" is applied, it adds 2 new squares, and each subsequent time this pattern is applied, only 1 new square is added. Therefore the total number of visited squares is 1 + 2 + 1 * 99 = 102.
"URLD" 100 Returns: 3
After each repetition of the pattern "URLD", the robot returns to the initial cell. So the answer here doesn't depend on times and is always equal to 3.
"UUDUDDLLDR" 1 Returns: 7
"UUDUDDLLDR" 12345678 Returns: 37037039
Submissions are judged against all 125 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class RobotSimulation with a public method int cellsVisited(string program, int times) · 125 test cases · 2 s / 256 MB per case