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

 

 

 

注意格式问题,每两个之间有一个空行,最后那个样例没有空行。

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=30;
struct node
{
    int up,down,right,left;
    bool operator ==(const node &tmp)
    {
        return up==tmp.up&&down==tmp.down&&right==tmp.right&&left==tmp.left;
    }
}mp[N];
int num[N],a[N][N];
bool ans;
void dfs(int p,int n)
{
    if(p==n*n||ans)
    {
        ans=true;
        return ;
    }
    int x=p/n;int y=p%n;
    for(int i=0;i<n*n&&!ans;i++)
    {
        if(num[i])
        {
            if(x>0)//不是第一行时,要考虑上三角形是否和上一行的下三角形相等
            {
                if(mp[i].up!=mp[a[x-1][y]].down)
                    continue;
            }
            if(y>0)//不是第一列时,要考虑左三角形是否和上一列的右三角形相等
            {
                if(mp[i].left!=mp[a[x][y-1]].right)
                    continue;
            }
            a[x][y]=i;
            --num[i];
            dfs(p+1,n);
            ++num[i];
        }
    }
}
int main()
{
    int n;
    int cas=1;
    while(cin>>n,n)
    {
        if(cas>1) printf("\n");//格式
        memset(num,0,sizeof(num));
     for(int i=0;i<n*n;i++)
        {
            cin>>mp[i].up>>mp[i].right>>mp[i].down>>mp[i].left;
            bool flag=false;
            for(int j=0;j<i;j++)//是否存在同样的正方形
            {
                if(num[j]&&mp[j]==mp[i])
                {
                    num[j]++;
                    flag=true;
                    break;
                }
            }
            if(!flag)
                    num[i]++;
        }
        memset(a,0,sizeof(a));
        ans=false;
        dfs(0,n);
        printf("Game %d: ",cas++);
        if(ans) printf("Possible\n");
        else printf("Impossible\n");
    }
    return 0;
}