ZOJ-1008 Gnome Tetravex

26 篇文章 0 订阅
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.1的分割线————————————————————

思路:这是一道DFS题,如果思考方向不对会感到很困难。那是因为这个矩阵需要自己“安排”,要尝试所有的摆放可能(不可旋转)。但是这么想太复杂了。换个思路,一个n×n的空矩阵,我从左上角第一个开始摆放,依次进行,只要符合题意就继续,不然就尝试下一个方块。这样每次摆放只需要看两个方向,即上和左。所有的方块存储成一维,即用即取。其实这样就不像是dfs了,倒是像暴力枚举了。枚举的不应该是图,应该是方块。

但是会超时。解决超时的办法,是把完全一样的方块统计归纳在一处,这样就不必重复尝试。但还是会超时,还需要剪枝,一旦找到了一个解,就不要再找其他的方案了。

PS:下次再Debug,先看括号的位置有没有错吧!

代码如下:

/*
ID: j.sure.1
PROG: 
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;
/****************************************/
const int N = 25;
struct Node
{
	int dir[4];
	bool operator == (const Node &tmp)
	{
		if(memcmp(dir, tmp.dir, sizeof(dir)) == 0)
			return true;
		return false;
	}
}node[N];
int quan[N], mat[5][5], n;
bool ans;

void dfs(int cur)
{
	if(ans || cur == n*n) {//剪枝
		ans = true;
		return ;
	}
	int x = cur / n, y = cur % n;
	for(int i = 0; i < n*n; i++) {
		if(quan[i]) {
			if(x > 0) {
				if(node[i].dir[0] != node[mat[x-1][y]].dir[2])
					continue;
			}
			if(y > 0) {
				if(node[i].dir[3] != node[mat[x][y-1]].dir[1])
					continue;
			}
			mat[x][y] = i;
			--quan[i];
			dfs(cur+1);//如果此位置放置i
			++quan[i];//不放置i,尝试别的
		}
	}
}

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("1008.in", "r", stdin);
	freopen("1008.out", "w", stdout);
	#endif
	int cas = 1;
	while(scanf("%d", &n), n) {
		if(cas > 1) puts("");
		memset(quan, 0, sizeof(quan));
		for(int i = 0; i < n*n; i++) {
			for(int d = 0; d < 4; d++) {
				scanf("%d", &node[i].dir[d]);
			}
			bool same = false;
			for(int j = 0; j < i; j++) {
				if(quan[j] && node[j] == node[i]) {
					quan[j]++;
					same = true;
					break;
				}
			}
			if(!same)
				quan[i]++;
		}
		ans = false;
		memset(mat, 0, sizeof(mat));
		printf("Game %d: ", cas++);
		dfs(0);
		if(ans)
			puts("Possible");
		else
			puts("Impossible");         
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值