TopCoder problem "TurningMaze" used in SRM 385 (Division I Level Two)

Problem Statement


Once again, you are stuck in a rectangular maze, represented by a String[] maze. Each character in each element of maze represents one room. Each room, naturally, has four sides, and each side may or may not have a door. The character representing the room will denote the set of doors present in that room. There will be four types of rooms: 'A' denotes a room with all four doors present, 'B' denotes a room with no doors, 'C' denotes a room with only north and south doors, and 'D' denotes a room with only east and west doors. The directions are chosen so that each element of maze represents a row of rooms from west to east, and those rows are ordered from north to south in maze.

You can move around the maze by walking from one room to one of the four adjacent rooms. However, you can do that only if both the room you are in and the room you are going into have the corresponding doors. For example, you can only move to the north if your room has a north door and the room to the north has a south door. The maze is surrounded by a solid wall, so you can never walk out of it, even if there's a door in a border room leading out. Every movement from one room to another takes 1 second.

The rooms having not enough doors may seriously constrain your movement in the maze. Luckily, you've been given a remote control that can change the maze. The remote control has only one button, and pressing this button makes all the rooms in the same row as your room and all the rooms in the same column as your room rotate 90 degress clockwise. Your room itself rotates twice; notice that a double rotation has the same effect as the room not being rotated at all. When a room rotates 90 degrees clockwise, its north door (or absence of such) becomes its east door (or absence of such), east becomes south, south becomes west and west becomes north. Operating the remote control also takes 1 second.

Return the least number of seconds required to get from the room at the northwest corner of the maze to the room at the southeast corner. If there's no way to achieve that, return -1.



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


-maze will contain between 2 and 7 elements, inclusive.
-Each element of maze will contain between 2 and 7 characters, inclusive.
-All elements of maze will contain the same number of characters.
-Each character in each element of maze will be one of 'A', 'B', 'C', 'D'.


{"AA", "AA"}
Returns: 2
We can walk anywhere we like.
{"AAA", "BBA", "AAA", "ABB", "AAA"}
Returns: 10
Returns: 8
Remote control comes into play.
Returns: 12
You can apply the remote control multiple times.
{"CA", "BA"}
Returns: -1
The remote control won't help if you're stuck in the initial cell.
{"CAA", "DAA", "ACA"}
Returns: 6

Problem url:

Problem stats url:




PabloGilberto , Olexiy , gawry , ivan_metelsky

Problem categories: