Problem:
Problem B: Lattice Practices

Once upon a time, there was a king who loved beautiful costumes very much. The king had a special cocoon bed to make excellent cloth of silk. The cocoon bed had 16 small square rooms, forming a 4 × 4 lattice, for 16 silkworms. The cocoon bed can be depicted as follows:

The cocoon bed can be divided into 10 rectangular boards, each of which has 5 slits:

Note that, except for the slit depth, there is no difference between the left side and the right side of the board (or, between the front and the back); thus, we cannot distinguish a symmetric board from its rotated image as is shown in the following:

Slits have two kinds of depth, either shallow or deep. The cocoon bed should be constructed by fitting five of the boards vertically and the others horizontally, matching a shallow slit with a deep slit.

Your job is to write a program that calculates the number of possible configurations to make the lattice. You may assume that there is no pair of identical boards. Notice that we are interested in the number of essentially different configurations and therefore you should not count mirror image configurations and rotated configurations separately as different configurations.

The following is an example of mirror image and rotated configurations, showing vertical and horizontal boards separately, where shallow and deep slits are denoted by '1' and '0' respectively.

Notice that a rotation may exchange position of a vertical board and a horizontal board.

Input

The input consists of multiple data sets, each in a line. A data set gives the patterns of slits of 10 boards used to construct the lattice. The format of a data set is as follows:

XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX XXXXX

Each x is either '0' or '1'. '0' means a deep slit, and '1' a shallow slit. A block of five slit descriptions corresponds to a board. There are 10 blocks of slit descriptions in a line. Two adjacent blocks are separated by a space.

For example, the first data set in the Sample Input means the set of the following 10 boards:

The end of the input is indicated by a line consisting solely of three characters "END".

Output

For each data set, the number of possible configurations to make the lattice from the given 10 boards should be output, each in a separate line.

Sample Input

10000 01000 00100 11000 01100 11111 01110 11100 10110 11110
10101 01000 00000 11001 01100 11101 01110 11100 10110 11010
END

Output for the Sample Input

40
6

Solution in JAVA (judge verdict: Accepted)

import java.util.HashSet;
import java.util.Scanner;

public class Main {

	static Scanner sc = new Scanner(System.in);
	static char[][] plate = new char[10][];
	static int[][] plateI = new int[10][2];
	static int ans;
	static HashSet<Integer> used = new HashSet<Integer>();

	public static void main(String[] args) {
		while (true) {
			plate[0] = sc.next().toCharArray();
			if (plate[0].length == 3) break;
			for (int i = 1; i < 10; ++i) {
				plate[i] = sc.next().toCharArray();
			}
			for (int i = 0; i < 10; ++i) {
				plateI[i][0] = plateI[i][1] = 0;
				for (int j = 0; j < 5; ++j) {
					if (plate[i][j] == '1') {
						plateI[i][0] += 1 << j;
						plateI[i][1] += 1 << (4 - j);
					}
				}
			}
			System.out.println(solve());
		}
	}

	static int solve() {
		ans = 0;
		used.clear();
		boolean[] selected = new boolean[10];
		dfs(0, 0, selected);
		return ans;
	}

	static void dfs(int state, int depth, boolean[] selected) {
		if (depth == 5) {
			finish(state, selected);
			return;
		}
		for (int i = 0; i < 10; ++i) {
			if (selected[i]) continue;
			selected[i] = true;
			dfs(state + ((0x1F - plateI[i][0]) << (depth * 5)), depth + 1, selected);
			if (plateI[i][0] != plateI[i][1]) {
				dfs(state + ((0x1F - plateI[i][1]) << (depth * 5)), depth + 1, selected);
			}
			selected[i] = false;
		}
	}

	static void finish(int state, boolean[] selected) {
		if (used.contains(state)) return;
		int[] rev = new int[5];
		for (int i = 0; i < 25; ++i) {
			if ((state & (1 << i)) != 0) {
				rev[i % 5] += 1 << (i / 5);
			}
		}
		//		System.out.println(Integer.toBinaryString(state));
		//		for (int i = 0; i < 5; ++i) {
		//			System.out.println(Integer.toBinaryString(rev[i]));
		//		}
		boolean[] rest = selected.clone();
		for (int i = 0; i < 5; ++i) {
			boolean found = false;
			for (int j = 0; j < 10; ++j) {
				if (!rest[j] && (rev[i] == plateI[j][0] || rev[i] == plateI[j][1])) {
					rest[j] = true;
					found = true;
					break;
				}
			}
			if (!found) return;
		}
		int all = (1 << 25) - 1;
		used.add(state);
		used.add(all - state);
		int mir = mirror(state);
		used.add(mir);
		used.add(all - mir);
		for (int i = 0; i < 3; ++i) {
			state = rotate(state);
			mir = rotate(mir);
			used.add(state);
			used.add(all - state);
			used.add(mir);
			used.add(all - mir);
		}
		++ans;
	}

	static int rotate(int v) {
		int ret = 0;
		for (int i = 0; i < 25; ++i) {
			if ((v & (1 << i)) != 0) {
				ret += 1 << ((i % 5) * 5 + (4 - i / 5));
			}
		}
		return ret;
	}

	static int mirror(int v) {
		int ret = 0;
		for (int i = 0; i < 25; ++i) {
			if ((v & (1 << i)) != 0) {
				ret += 1 << ((i / 5) * 5 + (4 - i % 5));
			}
		}
		return ret;
	}

	static void print(int v) {
		for (int i = 0; i < 5; ++i) {
			for (int j = 0; j < 5; ++j) {
				System.out.print((v >> (i * 5 + j)) & 1);
			}
			System.out.println();
		}
	}

}