ZOJ Problem Set - 1008

Gnome Tetravex

Time Limit: 10 Seconds      Memory Limit: 32768 KB

Hart is engaged in playing an interesting game, Gnome Tetravex, these days. In the game, at the beginning, the player is given n*n squares. Each square is divided into four triangles marked four numbers (range from 0 to 9). In a square, the triangles are the left triangle, the top triangle, the right triangle and the bottom triangle. For example, Fig. 1 shows the initial state of 2*2 squares.


Fig. 1 The initial state with 2*2 squares

The player is required to move the squares to the termination state. In the termination state, any two adjoining squares should make the adjacent triangle marked with the same number. Fig. 2 shows one of the termination states of the above example.


Fig. 2 One termination state of the above example

It seems the game is not so hard. But indeed, Hart is not accomplished in the game. He can finish the easiest game successfully. When facing with a more complex game, he can find no way out.

One day, when Hart was playing a very complex game, he cried out, "The computer is making a goose of me. It's impossible to solve it." To such a poor player, the best way to help him is to tell him whether the game could be solved. If he is told the game is unsolvable, he needn't waste so much time on it.


Input

The input file consists of several game cases. The first line of each game case contains one integer n, 0 <= n <= 5, indicating the size of the game.

The following n*n lines describe the marking number of these triangles. Each line consists of four integers, which in order represent the top triangle, the right triangle, the bottom triangle and the left triangle of one square.

After the last game case, the integer 0 indicates the termination of the input data set.


Output

You should make the decision whether the game case could be solved. For each game case, print the game number, a colon, and a white space, then display your judgment. If the game is solvable, print the string "Possible". Otherwise, please print "Impossible" to indicate that there's no way to solve the problem.

Print a blank line between each game case.

Note: Any unwanted blank lines or white spaces are unacceptable.


Sample Input

2
5 9 1 4
4 4 5 6
6 8 5 4
0 4 4 3
2
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
0


Output for the Sample Input

Game 1: Possible

Game 2: Impossible

 

1 #include <stdio.h>
2
3 typedef struct
4 {
5 int up, right, down, left;
6 int amount;
7 }SQ;
8 SQ SQUARE[25], KIND[25], tempSQUARE[25];
9
10  int N, AMOUNT, COLOR[25], JUMP, kindAMOUNT, TOP;
11
12  int switchSQUARE(int k)
13 {
14 return SQUARE[k].up + SQUARE[k].right*10 + SQUARE[k].down*100 + SQUARE[k].left*1000;
15 }
16
17 void sortSQUARE()
18 {
19 int i, j;
20 SQ temp;
21 for(i=0; i<AMOUNT-1; i++)
22 for(j=AMOUNT-1; j>i; j--)
23 if(switchSQUARE(j-1) > switchSQUARE(j))
24 {
25 temp = SQUARE[j];
26 SQUARE[j] = SQUARE[j-1];
27 SQUARE[j-1] = temp;
28 }
29
30 for(i=j=0; i<AMOUNT; i++)
31 {
32 KIND[j] = SQUARE[i];
33 KIND[j].amount = 1;
34 while(i<AMOUNT-1 && switchSQUARE(i) == switchSQUARE(i+1))
35 {
36 KIND[j].amount ++;
37 i++;
38 }
39 j++;
40 }
41 kindAMOUNT = j;
42 }
43
44 int judgeCASE(int L)
45 {
46 if(L/N > 0) /* up */
47 if(tempSQUARE[L].up != tempSQUARE[L-N].down)
48 return 0;
49 if(L%N > 0) /* left */
50 if(tempSQUARE[L].left != tempSQUARE[L-1].right)
51 return 0;
52 return 1;
53 }
54
55 void DFS(int L)
56 {
57 int k;
58
59 if(L >= AMOUNT)
60 {
61 JUMP = 1;
62 return;
63 }
64
65 for(k=0; k<kindAMOUNT; k++)
66 {
67 if(KIND[k].amount == 0) continue;
68 tempSQUARE[L] = KIND[k];
69 if(judgeCASE(L) == 1)
70 {
71 TOP ++;
72 KIND[k].amount --;
73 DFS(L+1);
74 if(JUMP == 1) return;
75 TOP --;
76 KIND[k].amount ++;
77 }
78 }
79 }
80
81 int main()
82 {
83 int i, count=0;
84
85 while(scanf("%d", &N) && N!=0)
86 {
87 AMOUNT = N*N;
88 JUMP = 0;
89 TOP = 0;
90 count ++;
91
92 for(i=0; i<AMOUNT; i++) /* 读取数据 */
93 scanf("%d%d%d%d", &SQUARE[i].up, &SQUARE[i].right, &SQUARE[i].down, &SQUARE[i].left);
94
95 sortSQUARE();
96
97 DFS(0);
98
99 if(count != 1) printf("\n");
100
101 if(TOP >= AMOUNT-1)
102 printf("Game %d: Possible\n", count);
103 else
104 printf("Game %d: Impossible\n", count);
105 }
106 return 0;
107 }
108

 

有两个关键的地方:一个是对方块进行分类,第二个是输出最后一个判断后不能带双回车

posted @ 2010-04-19 09:33  长江西岸  阅读(509)  评论(0编辑  收藏  举报