DWITE Online Computer Programming Contest

Now in 3D

December 2008
Problem 5

Let’s try to break out from the confines of the over-simplified 2D problems and add some depth to the otherwise typical maze problems.

The input file DATA5.txt will contain 5 sets of data. Each set starts out with a single integer 2 ≤ n ≤ 5 followed by n×n lines, describing a cube space. Pound sign (#) denotes solid space; period (.) denotes free space; capital “A” denotes the start; capital “B” denotes the end.

The output file OUT5.txt will contain 5 lines, each being the shortest distance between A and B in the input maze.

The maze traversal is done only through free space, in any of the 6 axial directions. There are no diagonal movements.

Sample input explanation, first set: A 2×2×2 empty cube, with A and B in two opposite corners. There are 6 different ways to get from A to B in 3 steps. There are also 3 different ways to get from A to B in 7 steps (without backtracking), but since we are looking for the shortest distance, the latter is of less interest.

Sample input explanation, second set: Also a 2×2×2 cube, but filled space forces only a single path to be available. Think of the path this way, starting at A: right, up one layer, down. Also 3 steps.

Sample input explanation, third set: A 3×3×3 cube. A and B are on empty layers, but they are separated by a mostly filled layer, with a single opening in its bottom-right corner.

Sample Input (first 3 of 5 shown):
2
A.
..
..
.B
2
A.
##
#.
#B
3
A..
...
...
###
###
##.
B..
...
...
Sample Output (first 3 of 5 shown):
3
3
10