Connection Status:
Competition Arena > AzimuthMonitoring
SRM 360 · 2007-07-24 · by darnley · Simple Math, String Manipulation
Class Name: AzimuthMonitoring
Return Type: int
Method Name: getAzimuth
Arg Types: (vector<string>)
Problem Statement

Problem Statement

A robot is moving in a plane following a set of instructions.

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 String[] containing the robot's instructions. Initially, the robot is facing north. Return the robot's final azimuth - the angle between north and the direction the robot is facing. Azimuth is always measured clockwise, and is a number between 0, inclusive, and 360, exclusive. For example, west corresponds to azimuth 270.

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.
Examples
0)
{"RIGHT"}
Returns: 90

Turning 90 degrees right changes the azimuth from 0 to 90.

1)
{"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.

2)
{"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.

3)
{"RIGHT 59", "RIGHT", "RIGHT", "HALT", "LEFT", "LEFT", "LEFT"}
Returns: 239

The fourth instruction is "HALT", thus none of the following "LEFT" instructions are executed.

4)
{"TURN AROUND", "HALT", "LEFT 5", "HALT", "LEFT 5", "HALT"}
Returns: 180

Note that there might be more then one "HALT" instruction.

16)
{"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.

Coding Area

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

Submitting as anonymous