TopCoder problem "BadMazeStrategy" used in TCO04 Round 1 (Division I Level One)

Problem Statement

    You will be given a String[] maze containing blank spots ('.' characters), walls ('X' characters), your character (a single 'Y' character), and the destination (a single 'D' character). You have implemented the following naive maze traversal strategy:
  • 1) Initially, set the current direction to Right.
  • 2) If you are on the destination spot, exit this process.
  • 3a) If taking 1 step in the current direction will lead to a blank spot or the destination take the step.
  • 3b) If taking 1 step in the current direction will lead to a wall or will leave the bounds of the maze, change the current direction by turning 45 degrees clockwise. For example, if the current direction is set to Right then turning 45 degrees clockwise will set the current direction to Down-Right.
  • 4) Go to step 2.
Character 0 is the leftmost spot in any element of maze. Furthermore, element 0 is the highest element of maze. Return the number of steps required to reach the destination, or -1 if this will never occur. A step is counted when you physically change spots but not when you simply change direction.


Method signature:int numSteps(String[] maze)
(be sure your method is public)


-If your character is at (element E, character C) of maze, and the current direction is Down-Right, then taking one step would leave you at (element E+1, character C+1) of maze. The other diagonal directions are treated analogously.
-The following sequence of directions are formed by consecutively turning 45 degrees clockwise:

Right, Down-Right, Down, Down-Left, Left, Up-Left, Up, Up-Right, Right.


-maze will contain between 1 and 50 elements inclusive.
-Each element of maze will contain between 1 and 50 characters inclusive.
-Each element of maze will contain the same number of characters.
-Each character in maze will be (quotes for clarity) '.', 'X', 'Y', or 'D'.
-maze will contain exactly one 'Y' and one 'D'.


Returns: 4
After taking 4 steps to the right, we reach the destination.
Returns: 7
After taking 4 steps to the right, continuing would lead into a wall. After changing direction to Down-Right we are still facing a wall, so we change directions again. Now facing downward we take 3 more steps and reach the destination.
Returns: -1
We keep running around the perimeter while the destination lies in the center.
Returns: 27
Don't run off the maze.
Returns: 39

Problem url:

Problem stats url:




PabloGilberto , lbackstrom

Problem categories: