OneDimensionalRobotEasy
SRM 608 · 2013-12-22 · by rng_58
SRM 608 · 2013-12-22 · by rng_58 · Simulation
Problem Statement
Problem Statement
A robot is placed on an infinitely long line. Initially the position of the robot is 0. Cat Snuke sends commands to move this robot.
You are given aString commands. For each i, the i-th character of commands (0-based index) represents the i-th command Snuke sends. If the i-th character of commands is 'R', the robot moves one unit to the right (i.e., from position x to position x+1). If this character is 'L', the robot moves one unit to the left (i.e., from position x to position x-1). The robot has a built-in safety mechanism that prevents it from going too far and losing the signal. The safety mechanism makes sure that the robot always stays between the positions -A and B, inclusive. If the robot receives the command 'R' when the robot is at B, or the command 'L' when the robot is at -A, the command will be ignored.
You are given theString commands and the int s A and B. Return the final position of the robot.
You are given a
You are given the
Constraints
- commands will contain between 1 and 50 characters, inclusive.
- Each character in commands will be either 'R' or 'L'.
- A and B will be between 1 and 50, inclusive.
Examples
0)
"RRLRRLLR" 10 10 Returns: 2
The robot will move as follows: 0 -> 1 -> 2 -> 1 -> 2 -> 3 -> 2 -> 1 -> 2.
1)
"RRRRR" 3 4 Returns: 4
The robot will move as follows: 0 -> 1 -> 2 -> 3 -> 4 -> 4.
2)
"LLLLLLLLLLR" 2 6 Returns: -1
The robot will move as follows: 0 -> -1 -> -2 -> -2 -> -2 -> -2 -> -2 -> -2 -> -2 -> -2 -> -2 -> -1.
3)
"RRLRRRRRRRRRRRRLR" 8 17 Returns: 13
4)
"LRRLRLRLLRLLLRRRRRLRLLRRRRLLLLRRLRRLRLLL" 4 38 Returns: 0
Submissions are judged against all 69 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class OneDimensionalRobotEasy with a public method int finalPosition(string commands, int A, int B) · 69 test cases · 2 s / 256 MB per case