ZOJ - 1008 Gnome Tetravex

Time Limit: 10000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu

[]   [Go Back]   [Status]  

Description

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

Source

Asia 2001, Shanghai (Mainland China)








dfs,刚开始看了有点莫名其妙。下面解释一下思路:
我们先从这些卡片中任意选取一个,放置在1行1列,然后选取下一个卡片放置到1行2列,放置的时候我们要像判断皇后(八皇后问题)是不是冲突一样判断这个卡片放置后是不是会满足相邻的卡片的相邻数字是不是相等,如果相等我们就继续放置下一个位置,如果不相等,那么就换下一个卡片放置,如果都不行,那么我们应该返回重新放置前面一个卡片,依次类推。。。。


代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int cur;//当前的方块标号
int count[30],map[30][4];//count[i]表示i方块的数量,map[][]用于存储方块信息(每个方块由四个三角形组成)
int result[30];//result[num] = i表示第i个方块可以放到num的位置


int dfs(int num,int n)
{
    int i;
    if(num==n*n) return 1;//如果全部放置成功,返回1
    for(i=0;i<cur;i++)//对于cur种方块遍历
    {
        if(!count[i]) continue;//如果这种方块用完了,继续
        if(num%n!=0)//如果不在第一列放置,需要满足新放置的方块的左边的数字等于它前一个的右边的数字(如果在第一列放置,没有限制)
        if(map[i][3]!=map[result[num-1]][1])
        continue;//如果不满足上述条件,说明这个方块不符合,继续寻找其他方块
        if(num/n!=0)//如果不在第一行放置,需要满足新放置的方块的上边的数字等于它上一个的下边的数字(如果在第一行放置,没有限制)
        if(map[i][0]!=map[result[num-n]][2])
        continue;
        result[num]=i;//第i个方块可以放到num的位置
        count[i]--;//放置成功,这种方块的数目减一
        if(dfs(num+1,n)) return 1;//如果深度遍历的结果都为1,说明全部放置成功(只有全部放置成功才会返回1)
        count[i]++;//只要有一个地方无法继续遍历,返回,记得i方块的数目要加回去
        result[num]=0;//写不写都可以,因为回溯的时候num不可能达到这里的num(num在变小,回溯嘛)
    }
    return 0;
}


int main()
{
    int n,i,j,a,b,c,d,ok=0,kase=0;
    while(scanf("%d",&n)&&n)
    {
        cur=0;
        memset(count,0,sizeof(count));//每次输入都记得把方块的信息清零
        for(i=0;i<n*n;i++)
        {
            scanf("%d%d%d%d",&a,&b,&c,&d);
            for(j=0;j<cur;j++)
            if(map[j][0]==a&&map[j][1]==b&&map[j][2]==c&&map[j][3]==d)
            {
                count[j]++;//合并完全相同的方块,剪枝,降低时间复杂度
                break;
            }
            if(j==cur)//如果这个方块跟之前的所有方块都不同,加入这个方块的信息
            {
                map[cur][0]=a;
                map[cur][1]=b;
                map[cur][2]=c;
                map[cur][3]=d;
                count[cur]++;//这种方块数目加一
                cur++;//方块总数加一
            }
        }
        if(!ok) ok=1;//这里的输出格式略坑,注意一下
        else printf("\n");
        kase++;
        if(dfs(0,n)) printf("Game %d: Possible\n",kase);
        else printf("Game %d: Impossible\n",kase);
    }
    return 0;
}


总结:时间还是有点不理想。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值