POJ1022_Packing Unit 4D Cubes_深搜

Packing Unit 4D Cubes
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2432 Accepted: 823

Description




We usually think that there are three geometric dimensions; the fourth dimension is usually time. However, the Association for Customizing Machines (ACM) has to deal with four geometrical dimensions for their strange customer EE3 who needs to pack four dimensional products into perpendicular parallelepipeds before shipping them to the newly emerged market niche just on the outskirts of the Milky Way. 
Each of EE3 products consists of a number of unit 4D cubes that are glued together at their faces. A face of a 4D cube is a 3D cube and each 4D cube has 8 such faces. The picture on the left shows a 4D cube projected into a plane with the four principal, orthogonal axes shown. It takes a bit of effort to stretch our imagination and see the faces of a 4D cube in such a projection. The pictures below try to illustrate how the two faces along each of the four axes are situated in 4D. Again, using just the planar projection it is not so easy to illustrate and takes some effort to see. But we have done a good job, didn't we? 


 
Each EE3 product to be packed consists of a number of unit 4D cubes that are glued together along their faces which are 3D cubes. Your job is simple: find the minimal volume measured in the number of unit 4D cubes of a perpendicular parallelepiped (a 4D box) into which the product can be packed before shipping.

Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by input data for each test case describing one EE3 product. The first line of each test case is an integer n (1 ≤ n ≤ 100) which is the number of unit 4D cubes used in the product. Next, there are n lines, each describing one unit cube and contains 9 nonnegative integer numbers. 
The first number, a positive integer, is the unique identifier of a cube and the remaining 8 numbers give the identities of neighbors of the cube listed in the following order: 
?the first two numbers are identifiers of the cubes glued to the opposing sides of the given cube along the x1 axis as seen looking in the direction of the x1 axis; 
?the next two numbers as above but for the x2 axis; 
?the next two numbers as above but for the x3 axis; 
?the next two numbers as above but for the x4 axis; 
If a cube does not have a neighbor glued to one of its faces we use 0 instead of a cube identifier. 
The problem is that the employees of ACM may produce inconsistent descriptions of EE3 products. There are two sources of such inconsistencies: 
?A consistent description must be symmetric, i.e. if cube x is glued to cube y at some face then cube y must be glued to cube x at the corresponding face along the same axis. The following description is inconsistent: 
3 0 0 1 0 0 0 0 0 
1 0 0 3 0 0 0 0 0 
?Any description must describe a single solid, i.e. there must be only one component in the product. Thus the following is inconsistent: 
1 2 0 0 0 0 0 0 0 
2 0 1 0 0 0 0 0 0 
3 0 0 4 0 0 0 0 0 
4 0 0 0 3 0 0 0 0

Output

There should be one output line per test case containing either the number of unit 4D cubes in the smallest 4D perpendicular parallelepiped oriented along the axes into which the product can be packed if the description is consistent, or the word Inconsistent, otherwise.

Sample Input

1
9
1 2 3 4 5 6 7 8 9
2 0 1 0 0 0 0 0 0
3 1 0 0 0 0 0 0 0
4 0 0 0 1 0 0 0 0
5 0 0 1 0 0 0 0 0
6 0 0 0 0 0 1 0 0
7 0 0 0 0 1 0 0 0
8 0 0 0 0 0 0 0 1
9 0 0 0 0 0 0 1 0

Sample Output

81

Source


大致题意:

这个题不算难,关键就是题意不好懂。这里我不介绍背景,从输入数据入手分析题意。

1.第一行表示样例数目。每个样例的第一行整数n表示样例中含有的物体数目,其后n行描述n个物体。

2.描述一个物体用一行,一行包含9个整数。第一个表示该物体的编号(相当于名字)。剩下的8个数字2个一组,共四组。

3.每一组数字表示该物体在一个方向上的前后邻居情况。(如果一个物体是二维平面的,我们认为它有x,y两个坐标轴方向。如果物体是三维的,我们认为它有x,y,z三个方向。本题中物体是四位的,用x,y,z,k代表它的四个方向)。

4.每一组中的第一个数表示这一方向上当前物体前面那个物体的编号。若当前物体的前面没有物体,则这个数为0.每一组中的第二个数表示这一方向上当前物体后面那个物体的编号,不存在则为0。

5.例如样例第三行:1 (2 3) (4 5) (6 7) (8 9) 。

含义如下:

当前物体编号为1;

在x方向上,前面物体编号为2,后面物体编号为3;

在y方向上,前面物体编号为4,后面物体编号为5;

······

7.所给数据有 合法/不合法 两种情况。数据合法的要求有两个

1)在某方向上,物体A 在 物体B 的前面,那么 物体B 必须在 物体A 同方向的后面。乍看很费解,结合

据清楚了。拿样例来说,1号物体的描述中,第二个数字是2 ,表示 2号 在 1号 x方向的前面。那么描

述 2号 物体时,表示 x轴 后面的数字必须是 1 ,即第三个数字是1。

2)给出的n个物体最终要连接成一个整体。(两物体在某一方向相邻,就认为它们连接起来了)。这个可

以测试连通性解决。

8.若数据 不合法 ,输出Inconsistent。若数据合法,则需要输出 该整体 的 外接立方体(四维的)的体积。

9.关于体积的求解:当我们求三维立方体的体积时,就是把x,y,z三个方向上的长度乘起来。对于本题中的四维立方体,只需要把x,y,z,k四个轴方向上的最大长度乘起来就可以了。每一个物体在四个方向的长度都是1.


大致思路:

总体上利用深度优先搜索,从任意一个物体出发,看看是否能够遍历全部n个物体。若能,则满足第二个合法条件。

在遍历的同时检查第一个合法条件。例如:从 物体A 出发进入 物体B 的瞬间,检查 物体B 描述数据的对应位置是不是 物体A 的编号,如果是则进入B,否则直接返回 数据非法标志。

在遍历的同时还可以确定 外接立方体 在四个方向上的长度。任意挑选的第一个物体坐标为(0,0,0,0,),开两个数组up[4],down[4],分别记录在四个方向上的 最大坐标值和最小坐标值。若搜索到的第二个物体在当前物体x方向的前面,则它的坐标为(1,0,0,0),若在后面则是(-1,0,0,0)。即时比较更新up,down两个数组的值,则遍历结束后up-down就是所需四个方向的长度,乘起来就是答案。


解题过程:

只要读懂了题,还是很简单的。


#include<cstdio>
#include<cstring>

int up [4],down [4],now[4];//up记录四个方向上的最大坐标,down记录最小,now记录当前所处物体的坐标
int unit [3000][9];//表示每个物体的各种数据。看到网上做过的聚聚说编号不超过1002,于是开了3000
int cas,n,no,cnt;

bool dfs (int x)
{
	unit[x][0]=1;//标记为已发现
	cnt++;//发现数加一

	int i=1;
	while(1){

		for(;i<9&&(!unit[x][i]||unit[unit[x][i]][0]);i++);//搜索未被发现的相邻物体

		if(i==9) return 1;//搜索完成,且过程中没有违反第一个合法条件
		int p=(i-1)/2,q=(i%2)?-1:1;//确定发现的物体与当前物体的位置关系
		if(unit[unit[x][i]][i-q]!=x) return 0;//判断第一个合法条件

		now[p]+=q;//进入下一个物体,并更新up和down
		if(now[p]>up[p]) up[p]=now[p];
		if(now[p]<down[p]) down[p]=now[p];

		if(!dfs(unit[x][i])) return 0;//下一层搜索中违反了第一个合法条件,本层也要返回假
		now[p]-=q;//还原并回溯
	}
}

int main ()
{
	scanf("%d",&cas);
	while(cas--){

		cnt=0;//cnt表示已经到达过的物体数,初始化为0
		for(int i=0;i<4;i++)
			up[i]=down[i]=now[i]=0;//全部初始化为0

		scanf("%d",&n);//该实例中的物体数
		for(int i=0;i<n;i++){
			scanf("%d",&no);//先输入编号
			unit[no][0]=0;//0号元素表示物体是否已发现,全部初始化为0表示未发现
			for(int j=1;j<9;j++)
				scanf("%d",&unit[no][j]);//输入物体的各项数据
		}

		if(dfs(no)&&cnt==n){//若cnt不等于n表示n个物体未连成整体,dfs返回假表示不满足第一个合法条件
			int ans=1;
			for(int i=0;i<4;i++)
				ans*=(up[i]-down[i]+1);//一个方向上的 最大值-最小值+1 就是这个方向上的长度
			printf("%d\n",ans);
		}
		else printf("Inconsistent\n");

	}

	return 0;

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值