#P17183. [ICPC 2017 Hong Kong R] Equivalence of Sudoku

[ICPC 2017 Hong Kong R] Equivalence of Sudoku

Problem Description

A Sudoku solution is one 9×99 \times 9 matrix in which each digit from 11 to 99 occurs only once every row, every column and every 3×33 \times 3 square. Given one Sudoku solution, SS, it is easy to generate many other equivalent solutions, by performing one or more of the elementary transformations: RR for a rotation by 90,18090, 180 or 270270 degrees, MM for a mirror image by flipping along the horizontal or vertical axis, BB for a bijective substitution of each of the digits 11 to 99 being mapped to another set of digits 11 to 99. It can be seen that S1,S2S_1, S_2, and S3S_3 are all equivalent to SS in the following example.

:::align{center} :::

A partial Sudoku is one in which not all of the 81 cells are filled up. We say that a partial Sudoku P1 is subsumed by (less than) another one P2 if there exists P which is equivalent to P2, and P is obtained from P1 by filling in one or more cells. In the following example, P1 is subsumed by P2 with respect to P, where P can be obtained from P2 via a bijective mapping {1,2,3,4,5,6,7,8,9}{2,9,3,7,8,6,1,5,4}\{1,2,3,4,5,6,7,8,9\} \to \{2,9,3,7,8,6,1,5,4\}, after rotating P2 clockwise by 90 degrees. Similarly, we say that P2 subsumes P1 or P2 is greater than P1 with respect to P. Note that it is not necessary that one of the matrices is completely filled.

:::align{center} :::

To summarize, between every two Sudoku matrices S1S_1 and S2S_2, there are four possible relationships: S1S_1 is equivalent to S2S_2 (EE), S1S_1 is less than S2S_2 (LL), S1S_1 is greater than S2S_2 (GG), S1S_1 is incomparable to S2S_2 (II). Write a program to read in a list of nn Sudoku matrices and determine the pairwise relationship between them. The output is an n×nn \times n matrix showing the relationships (E,L,G,I)(E, L, G, I). It is obvious that the diagonal elements are all EE (every matrix is equivalent to itself trivially).

Input Format

The first line contains the number of matrices nn. Each subsequent set of 99 lines corresponds to a (partial) Sudoku matrix. An unfilled cell is represented as 00 (instead of blank). Assume that 2<n4002 < n \le 400, all inputs are correct and all matrices are correct (partial) Sudoku matrices.

Output Format

For each pair of input matrices, determine the relationship between them and output that relationship as a single letter O{E,L,G,I}O \in \{E, L, G, I\}, one line for each input matrix.

3
0 7 4 5 2 3 0 9 8
0 0 0 7 9 8 1 4 0  
5 9 8 0 4 0 2 3 7  
6 3 0 2 7 9 5 8 0
8 4 7 0 5 6 9 2 3  
0 2 5 0 8 4 0 0 1  
4 8 6 9 1 0 3 5 2
2 1 9 8 3 5 0 7 6
0 5 3 4 0 2 8 0 0
0 0 8 0 5 0 4 7 0
6 1 5 2 0 0 9 8 0
0 3 0 9 0 0 5 0 2
0 0 0 6 7 1 0 9 0
4 6 0 8 9 2 0 0 0
9 0 0 0 0 0 0 0 1
1 0 2 0 8 9 7 6 5
8 0 6 5 1 4 2 3 9
0 0 9 7 2 0 1 4 0
1 7 4 5 2 3 0 9 8
3 0 2 7 9 0 1 4 5
5 9 8 0 4 0 2 3 7
6 3 0 0 7 9 5 8 4
8 4 7 0 5 6 9 0 3
9 2 5 3 8 4 7 6 0
4 8 6 9 0 0 3 5 2
2 1 9 0 3 5 4 7 6
0 5 3 4 0 0 8 1 9
EGI
LEL
IGE