ZOJ 1008(DFS+剪枝) Gnome Tetravex

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

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define    forl(i, a, b) for(int i = (a); i <  (b); ++i)
#define   forle(i, a, b) for(int i = (a); i <= (b); ++i)
#define    forg(i, a, b) for(int i = (a); i >  (b); --i)
#define   forge(i, a, b) for(int i = (a); i >= (b); --i)
#define rep(n)  for(int               repp = 0; repp <    (n); ++repp)
#define repc(n) for(int repp_b = (n), repp = 0; repp < repp_b; ++repp)
#define rst(a, v) memset(a, v, sizeof a)
#define cpy(a, b) memcpy(a, b, sizeof a)

int iSquare[25][4];       //游戏的方格矩阵
int iCount[25];         //存放方格的类型,最多为25个,重复的只算一个
int iTable[25];          //存放解决方案,实际放置的方格
int q,n;             //方格类型的数量和方格矩阵的大小
int Place(int iPos)
{
	int i;
	if(iPos==n*n)	return 1;
	for(i=0;i<q;i++)
	{
		if(iCount[i]==0)	continue;
		if(iPos%n!=0)                   //不在最左的方格,水平相邻的三角形相比
			if(iSquare[iTable[iPos-1]][1]!=iSquare[i][3])
				continue;
		if(iPos>=n)                     //不在最上方的方格,垂直相邻的三角形相比
			if(iSquare[iTable[iPos-n]][0]!=iSquare[i][2])
				continue;
		iTable[iPos]=i;
		iCount[i]--;          //该类型方格数量减少一个
		if(Place(iPos+1)==1)	return 1;
		iCount[i]++;          //回溯,返回原状态         
	} 
	return 0;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int top,right,left,bottom;
    int cas=0;
    while(scanf("%d",&n)!=EOF&&n!=0)
	{
		memset(iSquare,0,sizeof(iSquare));
		memset(iCount,0,sizeof(iCount));
		memset(iTable,0,sizeof(iTable));
		int i,j;
		q=0;
		for(i=0;i<n*n;i++)
		{
			scanf("%d%d%d%d",&top,&right,&bottom,&left);
			for(j=0;j<q;j++)
			{
				if(iSquare[j][0]==top&&iSquare[j][1]==right //如果方格类型是相同的
					&&iSquare[j][2]==bottom&&iSquare[j][3]==left)
				{
					iCount[j]++;    //该类型方格数加1
					break;
				}
			}
			if(j==q)            //如果读取的方格数与原来有的方格类型不重复,则新增加1
			{
				iSquare[j][0]=top;
				iSquare[j][1]=right;
				iSquare[j][2]=bottom;
				iSquare[j][3]=left;
				iCount[j]=1;        //该方格的类型加1
				q++;                //方格的总类型数量加1
			}
		}
		//for(int i=0;i<q;i++)
			//cout<<iTable[i]<<' '<<iCount[i]<<endl;
		if(cas)	printf("\n");     //注意输出格式,PE了4次。。。。。
		if(Place(0))
			printf("Game %d: Possible\n",++cas);
		else	printf("Game %d: Impossible\n",++cas);
	}
    return 0;
}


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


题目:10秒。。。可见数据之大,如果枚举的话(n*n)!最大数据量是5,-----------25!...采用DFS加剪枝,跑了6000+ms


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值