Problem:
Problem F: Dice Puzzle

Let’s try a dice puzzle. The rules of this puzzle are as follows.

Dice with six faces as shown in Figure 1 are used in the puzzle.

Figure 1: Faces of a die

With twenty seven such dice, a 3 × 3 × 3 cube is built as shown in Figure 2.

Figure 2: 3 × 3 × 3 cube

When building up a cube made of dice, the sum of the numbers marked on the faces of adjacent dice that are placed against each other must be seven (See Figure 3). For example, if one face of the pair is marked “2”, then the other face must be “5”.

Figure 3: A pair of faces placed against each other

The top and the front views of the cube are partially given, i.e. the numbers on faces of
some of the dice on the top and on the front are given.

Figure 4: Top and front views of the cube

The goal of the puzzle is to find all the plausible dice arrangements that are consistent
with the given top and front view information.

Your job is to write a program that solves this puzzle.

Input

The input consists of multiple datasets in the following format.

N
Dataset1
Dataset2
...
DatasetN

N is the number of the datasets.

The format of each dataset is as follows.

T11 T12 T13
T21 T22 T23
T31 T32 T33
F11 F12 F13
F21 F22 F23
F31 F32 F33

Tij and Fij (1 ≤ i ≤ 3, 1 ≤ j ≤ 3) are the faces of dice appearing on the top and front views,
as shown in Figure 2, or a zero. A zero means that the face at the corresponding position is
unknown.

Output

For each plausible arrangement of dice, compute the sum of the numbers marked on the nine
faces appearing on the right side of the cube, that is, with the notation given in Figure 2,

∑3i=1∑3j=1Rij.

For each dataset, you should output the right view sums for all the plausible arrangements, in
ascending order and without duplicates. Numbers should be separated by a single space.

When there are no plausible arrangements for a dataset, output a zero.

For example, suppose that the top and the front views are given as follows.

Figure 5: Example

There are four plausible right views as shown in Figure 6. The right view sums are 33, 36, 32,
and 33, respectively. After rearranging them into ascending order and eliminating duplicates,
the answer should be “32 33 36”.

Figure 6: Plausible right views

The output should be one line for each dataset. The output may have spaces at ends of lines.

Sample Input

4
1 1 1
1 1 1
1 1 1
2 2 2
2 2 2
2 2 2
4 3 3
5 2 2
4 3 3
6 1 1
6 1 1
6 1 0
1 0 0
0 2 0
0 0 0
5 1 2
5 1 2
0 0 0
2 0 0
0 3 0
0 0 0
0 0 0
0 0 0
3 0 1

Output for the Sample Input

27
24
32 33 36
0

Solution in Ruby (judge verdict: Accepted)

### constant

#     (+x,+y,+z)
DICE = [1, 2, 3]

### subroutines

def rot_x(dc0, n = 1)
  dc = dc0.clone
  n.times.each{tmp = dc[1]; dc[1] = 7 - dc[2]; dc[2] = tmp}
  dc
end

def rot_y(dc0, n = 1)
  dc = dc0.clone
  n.times.each{tmp = dc[2]; dc[2] = 7 - dc[0]; dc[0] = tmp}
  dc
end

def rot_z(dc0, n = 1)
  dc = dc0.clone
  n.times.each{tmp = dc[0]; dc[0] = 7 - dc[1]; dc[1] = tmp}
  dc
end

def right_sum()
  rsum = 0
  for x in (0...3)
    for y in (0...3)
      posz = x * 9 + y * 3
      d = $cbs[posz][2] | $cbs[posz + 1][2] | $cbs[posz + 2][2]
      rsum += d
    end
  end
  rsum
end

def rec(k)
  if k >= 27
    $rsums[right_sum()] = true
    return
  end

  x = k / 9
  y = (k / 3) % 3
  z = k % 3

  posx = k - x * 9
  posy = k - y * 3
  posz = k - z
  xd = $cbs[0 * 9 + posx][0] | $cbs[1 * 9 + posx][0] | $cbs[2 * 9 + posx][0]
  yd = $cbs[0 * 3 + posy][1] | $cbs[1 * 3 + posy][1] | $cbs[2 * 3 + posy][1]
  zd = $cbs[0 * 1 + posz][2] | $cbs[1 * 1 + posz][2] | $cbs[2 * 1 + posz][2]

  cbsk = $cbs[k]

  $alldcs.each do |dc|
    if (xd != 0 && xd != dc[0]) ||
       (yd != 0 && yd != dc[1]) ||
       (zd != 0 && zd != dc[2])
      next
    end

    $cbs[k] = dc
    rec(k + 1)
    $cbs[k] = cbsk
  end
end

### main

$alldcs = []

for zr in (0...4)
  for yr in (0...4)
    for xr in (0...4)
      $alldcs << rot_z(rot_y(rot_x(DICE, xr), yr), zr)
    end
  end
end
$alldcs.sort!.uniq!
$nalldcs = $alldcs.length
#p $alldcs

n = gets.to_i

n.times do
  $cbs = 27.times.map{[0] * 3}

  for y in (0...3)
    tys = gets.split.map(&:to_i)
    for z in (0...3)
      tyz = tys[z]
      next if tyz == 0
      for x in (0...3)
        pos = x * 9 + y * 3 + z
        $cbs[pos][0] = tyz
      end
    end
  end

  for x in (0...3)
    fxs = gets.split.map(&:to_i)
    for z in (0...3)
      fxz = fxs[z]
      next if fxz == 0
      for y in (0...3)
        pos = x * 9 + y * 3 + z
        $cbs[pos][1] = fxz
      end
    end
  end
  #p $cbs; exit

  $rsums = {}

  rec(0)

  rs = $rsums.keys.sort
  puts rs.empty? ? 0 : rs.join(' ')
end