Cubic Colonies

Time limit5sMemory limit128 MB

Summary
Given a 3x3x3 arrangement of unit cubic blocks (some missing) and two surface points, compute the length of the shortest path on the colony's outer surface between them, allowing passage through zero-width edge or vertex gaps.
Level

Hard9 of 10

Topics
Geometry, Graph, Shortest path, Implementation
Solved
No attempts yet

Problem

In AD 3456 the earth is too small for hundreds of billions of people to live on in peace. The Interstellar Colonization Project with Cubes (ICPC) moves people from the earth to space colonies to ease the crowding. ICPC obtained funding from governments and built space colonies very quickly and cheaply out of prefabricated cubic blocks.

The largest colony looks like a Rubik's cube. It is made of 3×3×33 \times 3 \times 3 cubic blocks (Figure J.1A). A smaller colony is missing some of those blocks.

A colony made of more than one block is manufactured like this. We start from a single block, then repeatedly glue a new block onto the blocks already placed so that their faces match exactly. Every pair of touching faces is glued.

Figure J.1: the largest colony and a smaller colony

Just before the first launch we found a design flaw. Every colony needs a cable that connects two points on its surface, and the inside of a prefabricated block cannot be changed in a short time. So we decided to attach the cable to the surface of the colony. Any part of the cable that is off the surface would be sheared off during the launch, so the whole cable has to lie on the surface. The budget is tight, so the cable has to be as short as possible. The dashed line in Figure J.1B is one such cable. Given the shape of a colony and two points on its surface, write a program that computes the length of the shortest possible cable for that colony.

Input

The input contains a series of datasets. Each dataset describes one colony and the two points on its surface in the following format.

x1 y1 z1 x2 y2 z2
b0,0,0 b1,0,0 b2,0,0
b0,1,0 b1,1,0 b2,1,0
b0,2,0 b1,2,0 b2,2,0
b0,0,1 b1,0,1 b2,0,1
b0,1,1 b1,1,1 b2,1,1
b0,2,1 b1,2,1 b2,2,1
b0,0,2 b1,0,2 b2,0,2
b0,1,2 b1,1,2 b2,1,2
b0,2,2 b1,2,2 b2,2,2

Each of the nine lines that describe the blocks consists of exactly three characters. The spaces above are there only to make the format readable.

(x1,y1,z1)(x_1, y_1, z_1) and (x2,y2,z2)(x_2, y_2, z_2) are two distinct points on the surface of the colony, and x1,x2,y1,y2,z1,z2x_1, x_2, y_1, y_2, z_1, z_2 are integers that satisfy 0≤x1,x2,y1,y2,z1,z2≤30 \le x_1, x_2, y_1, y_2, z_1, z_2 \le 3. bi,j,kb_{i,j,k} is # when a cubic block whose two opposite vertices are (i,j,k)(i, j, k) and (i+1,j+1,k+1)(i+1, j+1, k+1) is present, and . when it is absent. Figure J.1A corresponds to the first dataset of the example and Figure J.1B to the second one. A cable can pass through the zero-width gap between two blocks that touch only along their edges or at their vertices. In Figure J.2A, the third dataset of the example, the shortest cable runs from point A (0,0,2)(0, 0, 2) to point B (2,2,2)(2, 2, 2) through (1,1,2)(1, 1, 2), a point shared by six blocks. In Figure J.2B, the fourth dataset of the example, the shortest cable also goes through the gap between two blocks that are not glued to each other. When two blocks share only a single vertex, a cable may pass through that vertex (Figure J.2C, the fifth dataset of the example).

No colony consists of all 3×3×33 \times 3 \times 3 blocks except the center one.

Six zeros terminate the input.

Figure J.2: the dashed lines are the shortest cables. Some blocks are drawn partly transparent for illustration.

Output

For each dataset, print one line with the length of the shortest cable that connects the two given points, rounded to six digits after the decimal point. Always print all six digits.

The two given points can always be connected by a cable. No answer in the input data lies on a rounding boundary.

Examples1

  1. Example 1

    Input
    0 0 0 3 3 3
    ###
    ###
    ###
    ###
    ###
    ###
    ###
    ###
    ###
    3 3 0 0 0 3
    #..
    ###
    ###
    ###
    ###
    ###
    #.#
    ###
    ###
    0 0 2 2 2 2
    ...
    ...
    ...
    .#.
    #..
    ...
    ##.
    ##.
    ...
    0 1 2 2 1 1
    ...
    ...
    ...
    .#.
    #..
    ...
    ##.
    ##.
    ...
    3 2 0 2 3 2
    ###
    ..#
    ...
    ..#
    ...
    .#.
    ..#
    ..#
    .##
    0 0 0 0 0 0
    
    Expected output
    6.708204
    6.478709
    2.828427
    2.236068
    2.828427