ZOJ1008 Gnome Tetravex(DFS)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1008

题意:

哈特近来一直在玩有趣的 Gnome Tetravex 游戏。在游戏开始时,玩家会得到 n×n(n≤5) 个正方形。每个正方形都被分成 4个标有数字的三角形(数字的范围是 0到9)。这四个三角形分 别被称为“左三角形”、“右三角形”、“上三角形”和“下三角形”。例如,图 2.12(a)是2×2的正 方形的一个初始状态。

图2.12 Gnome Tetravex游戏 玩家需要重排正方形,到达目标状态。在目标状态中,任何两个相邻正方形的相邻三角形上 的数字都相同。图 2.12(b)是一个目标状态的例子。

判断是否有解。

题解:

DFS。一次确定每个格子填充什么样的方块。但是这样的话复杂度25!,TLE。为了能够减少时间,我们将相同类型的方块只保存一次,并且保存相同方块出现的次数。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX=6;
struct  part
{
	int l,r,u,d;
	part(){};
	part(int u,int r,int d,int l):u(u),r(r),d(d),l(l){};
	bool operator ==(const part &tmp)
	{
		return tmp.l==l&&tmp.r==r&&tmp.u==u&&tmp.d==d;
	}
};
part p[MAX*MAX];//记录输入的块
int sum[MAX*MAX];//每种块的个数
int g[MAX][MAX];
int n;
bool ans;
void dfs(int t)
{
	if(t==n*n||ans)
	{
		ans=true;
		return;
	}
	int x=t/n,y=t%n;
	for(int i=0;i<n*n&&!ans;i++)
	{
		if(sum[i])
		{
			if(x>0)
				if(p[i].u!=p[g[x-1][y]].d) continue;
			if(y>0)
				if(p[i].l!=p[g[x][y-1]].r) continue;
			g[x][y]=i;
			sum[i]--;
			dfs(t+1);
			sum[i]++;
		}
	}
}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int tcase=1;
	while(scanf("%d",&n)&&n)
	{
		if(tcase>1) printf("\n");
		printf("Game %d: ",tcase++);
		memset(sum,0,sizeof(sum));
		for(int i=0;i<n*n;i++)
		{
			int l,r,u,d;
			scanf("%d%d%d%d",&u,&r,&d,&l);
			p[i]=part(u,r,d,l);
			int flag=0;
			for(int j=0;j<i;j++)
			{
				if(p[j]==p[i])
				{
					flag=1;
					sum[j]++;
					break;
				}
			}
			if(!flag)
				sum[i]++;
		}
		ans=false;
		dfs(0);
		printf("%s\n",ans?"Possible":"Impossible");
	}
	return 0;
}
时间: 2024-08-28 13:41:25

ZOJ1008 Gnome Tetravex(DFS)的相关文章

zoj 1008 Gnome Tetravex (dfs+枚举)

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 m

ZOJ 1008 Gnome Tetravex(DFS)

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 m

ZOJ Problem Set - 1008 Gnome Tetravex (DFS+剪枝)

ZOJ Problem Set - 1008 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 divide

[ZOJ 1008]Gnome Tetravex (dfs搜索 + 小优化)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1008 题目大意:给你n*n的矩阵,每个格子里有4个三角形,分别是上右下左,每个三角形里面标记了数字,问你能否通过移动这n*n个格子,使得相邻两个三角形具有相同的数字? dfs暴搜,dfs(x,y)代表当前要放(x,y)这个位置. 然后枚举给定的每个格子. 如果说可以放,就dfs下一个格子. 这一道题需要把相同的格子压缩起来,也就是说为了节省时间,可以记录相同的格

ZOJ1008 Gnome Tetravex

DFS+剪枝~ #include<bits/stdc++.h> using namespace std; int a[30][4]; int N; int cnt; int c[30]; int p[30]; unordered_map<long long,int> pos; bool dfs (int v) { if (v==N*N) return true; for (int i=0;i<cnt;i++) { if (c[i]==0) continue; if (v%N!

ZOJ 1008 Gnome Tetravex (DFS + 剪枝)

Gnome Tetravex 题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=8 题意:有N*N个方格,每个方格分为上下左右四个部分,每个部分填数字.现在要求重排方块,使得每两个有边相连的方块对应的数字相同. 思路:就是一个简单的搜索,我想了个剪枝,将上下左右四个方向上每个数字对应的是哪几个方块记录下来,但是这个剪枝并没有起很大的作用,还是T了.后来才发现,如果有很多个方块是相同的,会重复搜索,所以需要将相同的方块一起处

zoj 1008 Gnome Tetravex

开放式存储阵列为每平方米有几个,否则,超时-- #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <st

1008 Gnome Tetravex

练习使用DPS的题,不知道有无别的做法,思路不复杂.形式是统计并且进行数字配对. 1 #include <stdio.h> 2 3 int m,n,f,sub[25][4],note[25],ans[25]; 4 5 void ini(){ 6 int i,j,top,right,bottom,left; 7 for(i=0;i<25;i++){ 8 for(j=0;j<4;j++) 9 sub[i][j]=0; 10 note[i]=0; 11 ans[i]=0; 12 } 13

ZOJ1008

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 m