poj 1022 Packing Unit 4D Cubes——dfs 搜索

转载于:(●’◡’●)

Packing Unit 4D Cubes
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

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

  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;

······

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

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

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

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

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

以测试连通性解决。

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

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

大致思路:

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

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

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

c++ AC 代码

#include<cstdio>
#include<iostream>
#include<cstring>
#define MAX 2010

int n,cube[MAX][10],forw[5],backw[5],now[5],id,cnt = 0;

bool dfs(int k)
{
	cube[k][0] = 1; cnt++;	// 0号位置表示该物品搜索过了

	for(int i=1;i<9;i++)
	{
		int u = cube[k][i];
		int v = cube[u][0];
		if(u == 0 || v == 1)  continue; // 已经访问过了,或者不存在
		int p = (i-1)/2, q = (i%2) ? -1 : 1;	// 记1、3、5、7为正面

		if(cube[u][i-q] != k) return false; // 不满足对称性

		now[p] += q;
		if(now[p] > forw[p]) forw[p] = now[p];	// now[i]的值为 正数 代表是正面
		if(now[p] < backw[p]) backw[p] = now[p]; // now[i]为 负数 代表背面

		if(!dfs(u)) return false;
		now[p] -= q; // 回溯
	}
	return true; // 遍历完所有
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=0;i<5;i++)
			forw[i] = backw[i] = now[i] = 0;
		for(int i=1;i<n+1;i++)
		{
			scanf("%d",&id);
			cube[id][0] = 0;
			for(int j=1;j<9;j++)
				scanf("%d",&cube[id][j]);
		}

		cnt = 0;
		if(dfs(id) && cnt == n)
		{
			int ans = 1;
			for(int i = 0;i<4;i++)
				ans *= (forw[i] - backw[i] + 1);
			printf("%d\n",ans);
		}
		else puts("Inconsistent");
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值