AzimuthMonitoring
SRM 360 · 2007-07-24 · by darnley
Problem Statement
The following instructions are possible (quotes for clarity only):
"LEFT" - turn left 90 degrees
"RIGHT" - turn right 90 degrees
"TURN AROUND" - turn around (a 180 degree turn)
"LEFT X" - turn left X degrees, where X is a positive integer
"RIGHT X" - turn right X degrees, where X is a positive integer
"HALT" - stop executing instructions; further instructions are not executed
You are given a
Notes
- Turning right corresponds to turning clockwise, turning left corresponds to turning counterclockwise.
Constraints
- instructions will contain between 1 and 50 elements, inclusive.
- Each element of instructions will be a valid instruction, as listed in the problem statement.
- In an instruction of the form "LEFT X" or "RIGHT X", X will be an integer between 1 and 179, inclusive, with no leading zeroes.
{"RIGHT"}
Returns: 90
Turning 90 degrees right changes the azimuth from 0 to 90.
{"LEFT", "LEFT", "TURN AROUND"}
Returns: 0
After turning left, the robot is facing west (azimuth 270). After turning left again, the robot is facing south (azimuth 180). Finally, turning around makes the robot face north, azimuth 0.
{"LEFT 5", "RIGHT 10", "LEFT 15", "RIGHT 20", "LEFT 25", "RIGHT 30", "LEFT 35", "RIGHT 40"}
Returns: 20
There are four pairs of instructions, each pair making the robot turn right 5 degrees.
{"RIGHT 59", "RIGHT", "RIGHT", "HALT", "LEFT", "LEFT", "LEFT"}
Returns: 239
The fourth instruction is "HALT", thus none of the following "LEFT" instructions are executed.
{"TURN AROUND", "HALT", "LEFT 5", "HALT", "LEFT 5", "HALT"}
Returns: 180
Note that there might be more then one "HALT" instruction.
{"LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "HALT"}
Returns: 270
After the first 4 turns the robot is facing north again, and the fifth instruction turns him west. The last instruction doesn't change anything.
Submissions are judged against all 94 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class AzimuthMonitoring with a public method int getAzimuth(vector<string> instructions) · 94 test cases · 2 s / 256 MB per case