TopCoder problem "CubeWalking" used in SRM 446 (Division I Level One , Division II Level Two)



Problem Statement

    

Consider the 3x3x3 cube shown above. There are nine squares on each of its six faces, and each face is colored using the following pattern:

  • The four corner squares are red.
  • The center square is green.
  • The remaining four squares are blue.

There is a robot standing in the green square of the top face of the cube, facing one of the blue squares. It receives a sequence of commands. Each command is one of the following:

  • 'L': Stay in the current square and turn left 90 degrees.
  • 'R': Stay in the current square and turn right 90 degrees.
  • 'W': Walk forward in the current direction to the next square.

Note that the robot can cross an edge of the cube into another face. When that happens, the cube will rotate automatically to keep the robot on the top face.

You are given a String movement containing the sequence of commands received by the robot. The robot will execute all of the commands in order. Return the color of the robot's final landing square - "RED", "GREEN" or "BLUE" (all quotes for clarity).

 

Definition

    
Class:CubeWalking
Method:finalPosition
Parameters:String
Returns:String
Method signature:String finalPosition(String movement)
(be sure your method is public)
    
 

Notes

-The answer does not depend on the initial direction of the robot.
 

Constraints

-movement will contain between 1 and 50 characters, inclusive.
-Each character in movement will be 'L', 'R' or 'W'.
 

Examples

0)
    
"LLRR"
Returns: "GREEN"
In this example, the robot only turns left or right without ever moving to a different square.
1)
    
"WWWWWWWWWWWW"
Returns: "GREEN"
Walking 12 squares forward in the same direction will lead the robot back to its original position.
2)
    
"WLWRW"
Returns: "RED"
3)
    
"WWLLWRWLWLLRWW"
Returns: "BLUE"

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=10515

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=13900&pm=10515

Writer:

crazyb0y

Testers:

PabloGilberto , ivan_metelsky , Chmel_Tolstiy , MThorn

Problem categories:

Simulation