PROBLEM STATEMENT
Create a method that instructs two robots to move about a grid of hexagons and
returns the distance between the robots after the robots have moved. The robots
move about a grid of hexagons arranged as diagrammed below. The grid extends
infinitely in all directions. The two robots start in the same hexagon facing
in the same direction. Each robot is given a sequence of commands that causes
it to move about the grid. After both robots have moved, they will be a certain
distance apart as measured in hexagons. The distance may be zero if both robots
end on the same hexagon. The following diagram shows a small portion of a grid
and the arrangement of hexagons. The lettering scheme is arbitrary and has no
significance other than as an aid to the explanations that follow.
___ ___
___/ b \___/ d \___ distance from a to b is 1
/ a \___/ c \___/ e \ distance from a to c is 2
\___/ g \___/ i \___/ distance from a to d is 3
/ f \___/ h \___/ j \ distance from d to f is 3
\___/ k \___/ l \___/ distance from f to j is 4
\___/ \___/
Movement Rules:
Each robot maintains a heading, the direction in which the robot is facing, and
a position, the hexagon that the robot occupies. Each robot is supplied a
command sequence. For each character in the robot's command sequence, the robot
behaves as follows:
'F' The robot moves forward one hexagon in the direction of its current heading.
'L' The robot changes its heading by rotating 60 degrees to the left
(counterclockwise) of the robot's current heading.
'R' The robot changes its heading by rotating 60 degrees to the right
(clockwise) of its current heading.
DEFINITION
Class Name: HexGrid
Method Name: distanceApart
Parameters: String, String
Returns: int
Method signature (be sure your method is public): int distanceApart(String
movesA, String movesB);
TopCoder will ensure the following criteria:
- movesA and movesB are both 1 to 50 characters in length, inclusive
- they will consist of only the characters F, L, and R (case-sensitive).
NOTES
* The robots move freely about the grid and do not interfere with each other in
anyway.
* The distance returned is always the minimal pathlength, as measured in
hexagons, between the final two robot positions.
* The robots can occupy the same hexagon at any given time.
EXAMPLES
distanceApart("FFRF", "LLF")
Using the diagram above, assume that two robots, A and B, initially start in
hexagon 'g' facing towards hexagon 'c'. The actual starting hexagon and initial
direction are irrelevant because the starting hexagon and initial direction are
the same for each robot.
The command sequence for Robot A is "FFRF" which contains four commands.
F: Robot A moves forward from g to c
F: Robot A moves forward from c to d
R: Robot A changes its heading by rotating 60 degrees to the right (clockwise)
F: Robot A moves forward from d to e.
The command sequence for Robot B is "LLF" which contains three commands.
L: Robot B rotates left (counterclockwise) 60 degrees
L: Robot B rotates left (counterclockwise) 60 degrees
F: Robot B moves forward from g to a
Robot A's final position is e. Robot B's final position is a. The distance from
e to a is 4. The method returns 4.
distanceApart("FFFF", "RRRF") ==> 5
distanceApart("FRF", "RFLF") ==> 0
distanceApart("FFFFFFFRFFFFFF", "LFFFFLFFF") ==> 16