poj1022 图的bfs

来源:互联网 发布:wamp mysql使用教程 编辑:程序博客网 时间:2024/06/05 19:17

 

如题:http://poj.org/problem?id=1022

 

Packing Unit 4D Cubes
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 1964 Accepted: 626

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

191 2 3 4 5 6 7 8 92 0 1 0 0 0 0 0 03 1 0 0 0 0 0 0 04 0 0 0 1 0 0 0 05 0 0 1 0 0 0 0 06 0 0 0 0 0 1 0 07 0 0 0 0 1 0 0 08 0 0 0 0 0 0 0 19 0 0 0 0 0 0 1 0

Sample Output

81

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest

 

题目大意:题目比较难理解、首先我们知道一个4d的立方体由8个面组成,分别是x1轴2个面,x2轴2个面,x3轴2个面,x4轴2个面。

题目第一行给出案例数t。之后给出t组案例的数据。

每一个案例第一行是n,代表这个产品由多少个上述的4d立方体构成。

然后n行,第i行第1个数是这个组成产品的一个零件(4d立方体)的编号,接下来给出8个数,前两个数代表在x1轴的2个面贴上去的4d立方体的编号。其余6个数同上,x1,x2,x3。

要输出的是至少需要一个体积多大的4d立方体盒子才能装下这个产品。

 

思路:首先注意,编号是打乱的,其次给出的图可能不能连通,比如1的某一面连2,3的某一面连4,就构成不了一个产品。

           其次注意,可能有顶点的不对应,比如1的x1轴第一个面连2,然而2的x1轴第二个面连的不是1.

           然后就是bfs搜索8个方向的最大长度,然后类比3d的正方体体积x*y*z,这个体积就是x1*x2*x3*x4.

          由于不知道最大的编号能达到多少,map<int,int>用的很好

 

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
//#define Max(a,b)(a>b?a:b)

int G[105][10];
int n;
int step[105][10]; //step[i][j]:当前编号i的正方体距离第一个正方体j方向的偏移量。
map<int,int>pos;

int bfs()
{
 queue<int>q;
 int vis[105]={0};
 q.push(1); //push的是映射过的i
 vis[1]=1;
 int i,j;
 while(!q.empty())
 {
  int now=q.front();
  q.pop();
  for(i=1;i<=8;i++)
  {
   int next=pos[G[now][i]];
   if(G[now][i]&&pos[G[next][(i%2==0?(i-1):(i+1))]]!=now) //?
    return 0;
   if(G[now][i]&&!vis[next])
   {
    for(j=1;j<=8;j++)
     step[next][j]=step[now][j];
    step[next][i]=step[now][i]+1;
    q.push(next);
    vis[next]=1;
   }
  }
 }
 for(i=1;i<=n;i++)
  if(!vis[i])
   return 0;
 return 1;
}

int main()
{
// freopen("C:\\1.txt","r",stdin);
 int t;
 cin>>t;
 int i,j;
 while(t--)
 {
  memset(G,0,sizeof(G));
  memset(step,0,sizeof(step));
  pos.clear();
  cin>>n;
  for(i=1;i<=n;i++)
  {
   int bianhao;
   scanf("%d",&bianhao);
   pos[bianhao]=i;
   for(j=1;j<=8;j++)
    scanf("%d",&G[i][j]);
  }
  if(!bfs())
  {
   printf("Inconsistent\n");
   continue;
  }
  int res=1;
  int w[9]={0};
  for(i=1;i<=8;i++)
  {
   int max=0;
   for(j=1;j<=n;j++)
   {
    if(step[j][i]>max)
     max=step[j][i];
   }
   w[i]=max;
  }
  for(i=1;i<=8;i+=2)
   res*=(w[i]+w[i+1]+1);
  printf("%d\n",res);
 }
 return 0;
}

 

0 0
原创粉丝点击