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 C++14 (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

struct Dice
{
	int top, right, front;
	Dice() :top(1), right(2), front(3) {};
	Dice(int t, int r, int f) :top(t), right(r), front(f) {};
	void set(int t, int r, int f) { top = t, right = r, front = f; }
	int get_top() { return top; }
	int get_bottom() { return 7 - top; }
	int get_right() { return right; }
	int get_left() { return 7 - right; }
	int get_front() { return front; }
	int get_back() { return 7 - front; }
	void to_front() { swap(front, top); top = 7 - top; }
	void to_back() { REP(i, 3) to_front(); }
	void to_right() { swap(top, right); top = 7 - top; }
	void to_left() { REP(i, 3) to_right(); }
	void to_clock() { swap(front, right); right = 7 - right; }
	void to_counter() { REP(i, 3) to_clock(); }
};


bool operator<(const Dice& a, const Dice& b)
{
	int at = a.top, bt = b.top;
	int af = a.front, bf = b.front;
	int ar = a.right, br = b.right;
	if (at != bt) return at < bt;
	if (af != bf) return ar < br;
	if (ar != br) return af < bf;
	return false;
}

struct Cube
{
	Dice dice[3][3][3];
	Cube()
	{
		REP(i, 3)REP(j, 3)REP(k, 3) dice[i][j][k].set(-1, -1, -1);
	}
};

const int dx[] = { -1,0,0,0,0,1 };
const int dy[] = { 0,-1,0,1,0,0 };
const int dz[] = { 0,0,-1,0,1,0 };
int state(int x, int y, int z)
{ 
	if (z == 3) return 1;
	else if (x == -1) return 2;
	else if(x >= 0 && y >= 0 && z >= 0 && x < 3 && y < 3 && z < 3) return 0; 
	else return 3;
}

set<Dice> pat;

void init()
{
	Dice d(1, 3, 2);
	vector<Dice> v;
	REP(i, 4)
	{
		d.to_front();
		v.push_back(d);
	}
	REP(i, v.size())
	{
		Dice tmp = v[i];
		REP(j, 4)
		{
			tmp.to_right();
			pat.insert(tmp);
		}
		REP(k, 4)
		{
			tmp.to_clock();
			pat.insert(tmp);
		}
	}
}

int main()
{
	cin.sync_with_stdio(false); cout << fixed << setprecision(10);
	init();
	int n; cin >> n;
	REP(cs, n)
	{
		vector<vi> top(3, vi(3)), front(3, vi(3));
		REP(i, 3)REP(j, 3) cin >> top[i][j];
		REP(i, 3)REP(j, 3) cin >> front[i][j];
		Cube cube;
		set<int> ans;
		queue<tuple<Cube, int, int, int>> que;
		que.emplace(cube, 0, 0, 0);
		while (!que.empty())
		{
			Cube tmp; int x, y, z; 
			tie(tmp, x, y, z) = que.front();
			que.pop();
			int tt = -1, tf = -1, tr = -1;
			bool flag = true;
			REP(i, 6)
			{
				int nx = x + dx[i], ny = y + dy[i], nz = z + dz[i];
				int st = state(nx, ny, nz);
				if (st == 1)
				{
					if (front[x][y] != 0)
					{
						if(tf != -1 && tf != front[x][y]) flag = false;
						tf = front[x][y];
					}
				}
				else if (st == 2)
				{
					if (top[z][y] != 0)
					{
						if (tt != -1 && tt != top[z][y]) flag = false;
						tt = top[z][y];
					}
				}
				else if (st == 0 && tmp.dice[nx][ny][nz].get_top() != -1)
				{
					if (i == 0)
					{
						if (tt != -1 && tt != tmp.dice[nx][ny][nz].get_top()) flag = false;
						tt = tmp.dice[nx][ny][nz].get_top();
					}
					if (i == 1) tr = tmp.dice[nx][ny][nz].get_right();
					if (i == 2)
					{
						if (tf != -1 && tf != tmp.dice[nx][ny][nz].get_front()) flag = false;
						tf = tmp.dice[nx][ny][nz].get_front();
					}
					if (i == 3) tr = tmp.dice[nx][ny][nz].get_right();
					if (i == 4) tt = tmp.dice[nx][ny][nz].get_front();
					if (i == 5) tt = tmp.dice[nx][ny][nz].get_top();
				}
			}
			if (!flag) continue;
			int nx = x, ny = y, nz = z;
			nz++;
			if (nz == 3) { nz = 0, ny++; }
			if (ny == 3) { ny = 0, nx++; }
			for (auto d : pat)
			{
				if (tt != -1 && tt != d.get_top()) continue;
				if (tf != -1 && tf != d.get_front()) continue;
				if (tr != -1 && tr != d.get_right()) continue;
				Cube nc = tmp;
				nc.dice[x][y][z] = d;
				if (x == 3)
				{
					int sum = 0;
					REP(i, 3)REP(j, 3) sum += nc.dice[i][2][j].get_right();
					ans.insert(sum);
				}
				else
				{
					que.emplace(nc, nx, ny, nz);
				}
			}
		}
		vi ansv;
		for (auto i : ans) ansv.push_back(i);
		if (ansv.size() == 0) cout << 0 << endl;
		else REP(i, ansv.size()) cout << ansv[i] << (i == ansv.size() - 1 ? "\n" : " ");
	}
	return 0;
}