zoj1008

来源:互联网 发布:windows 10 使用手册 编辑:程序博客网 时间:2024/05/29 17:35
 
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


#include <cstring>#include<cstdio>#define RE(a) memset(a,0,sizeof(a))#define RT(a) memset(a,-1,sizeof(a))using namespace std;int num[28][4], n, t, cur;int mp[7][7];int dir[4][2] = {-1,0,0,1,1,0,0,-1};int ref[7];
int amount[28];
//ref用来得到矩形临近                   0         0
//的矩形所对应的三角形的编号           3     1   3    1
//比如当前矩形的右边的三角形编号1         2         2
//对应右边矩形左边的三角形编号为3  
bool ok(int x,int y,int th){    for(int i = 0; i < 4; i ++)    {        int tx,ty,tf;        tx = x + dir[i][0];        ty = y + dir[i][1];        if(tx < 0 || tx >= n || ty < 0 || ty >= n)continue;        if(mp[tx][ty] == -1)continue;        int th2 = mp[tx][ty];        tf = ref[i];        if(num[th][i] != num[th2][tf])return false;    }    return true;}bool dfs(int cnt)//深搜按照从左到右,从上到下摆放矩形,摆到某个不符合要求就回溯,当然也就是剪枝了。{    int x = cnt/n,y = cnt % n;    for(int i = 0; i < cur; i ++)    {        if(amount[i] > 0 && ok(x,y,i))        {            amount[i]--;            mp[x][y] = i;            if(cnt + 1 == t)return true;//这里一般都写开头,但是考虑开一个栈空间马上退出,
//浪费时间了,还不如不开,就写这里了            if(dfs(cnt + 1))return true;            amount[i]++;            mp[x][y] = -1;        }    }    return false;}int main(){    int I = 1;    ref[0] = 2;    ref[1] = 3;    ref[2] = 0;    ref[3] = 1;    while(~scanf("%d",&n) && n)    {        t = n*n;        int a,b,c,d;        cur = 0;        for(int i = 0; i < t; i ++)        {            scanf("%d%d%d%d",&a,&b,&c,&d);            bool f = true;            for(int j = 0; j < cur; j ++)            {                if(a == num[j][0] && b == num[j][1] && c == num[j][2] && d == num[j][3])                {                    amount[j]++;//相同矩形用一个数组存储数量,这样也是一定程度得剪枝,                    f = false;  //使得同一个位置只尝试放一种矩形                    break;                }            }            if(f)            {                amount[cur] = 1;                num[cur][0] = a;                num[cur][1] = b;                num[cur][2] = c;                num[cur ++][3] = d;            }        }        RT(mp);        if(I > 1)printf("\n");//不这样输出空行,最后会多输出一行,格式错误        if(dfs(0))            printf("Game %d: Possible\n",I++);        else printf("Game %d: Impossible\n",I++);    }    return 0;}
原创粉丝点击