### 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.

### Definition

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

### Constraints

-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'.

### Examples

0)

 `{"AA", "AA"}`
`Returns: 2`
 We can walk anywhere we like.
1)

 `{"AAA", "BBA", "AAA", "ABB", "AAA"}`
`Returns: 10`
 Zig-zag.
2)

 `{"AAACAAA", "BBBBBBA"}`
`Returns: 8`
 Remote control comes into play.
3)

 `{"ACDCDCA", "BBBBBBA"}`
`Returns: 12`
 You can apply the remote control multiple times.
4)

 `{"CA", "BA"}`
`Returns: -1`
 The remote control won't help if you're stuck in the initial cell.
5)

 `{"CAA", "DAA", "ACA"}`
`Returns: 6`

#### Problem url:

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

#### Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10810&pm=8471

Petr

#### Testers:

PabloGilberto , Olexiy , gawry , ivan_metelsky

Search