HDU3231 Box Relations(拓扑排序)经典

来源:互联网 发布:铣削力计算软件 编辑:程序博客网 时间:2024/05/22 00:23

Box Relations

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1042    Accepted Submission(s): 389
Special Judge


Problem Description
There are n boxes C1, C2, ..., Cn in 3D space. The edges of the boxes are parallel to thex, y or z-axis. We provide some relations of the boxes, and your task is to construct a set of boxes satisfying all these relations.

There are four kinds of relations (1 <= i,j <= n, i is different fromj):
  • I i j: The intersection volume of Ci and Cj is positive.
  • X i j: The intersection volume is zero, and any point inside Ci has smallerx-coordinate than any point inside Cj.
  • Y i j: The intersection volume is zero, and any point inside Ci has smallery-coordinate than any point inside Cj.
  • Z i j: The intersection volume is zero, and any point inside Ci has smallerz-coordinate than any point inside Cj.
.
 

Input
There will be at most 30 test cases. Each case begins with a line containing two integersn (1 <= n <= 1,000) and R (0 <= R <= 100,000), the number of boxes and the number of relations. Each of the followingR lines describes a relation, written in the format above. The last test case is followed byn=R=0, which should not be processed.
 

Output
For each test case, print the case number and either the word POSSIBLE or IMPOSSIBLE. If it's possible to construct the set of boxes, thei-th line of the following n lines contains six integers x1, y1, z1, x2, y2, z2, that means thei-th box is the set of points (x,y,z) satisfying x1 <= x <= x2, y1 <= y <= y2, z1 <= z <= z2. The absolute values ofx1, y1, z1, x2, y2, z2 should not exceed 1,000,000.

Print a blank line after the output of each test case.
 

Sample Input
3 2I 1 2X 2 33 3Z 1 2Z 2 3Z 3 11 00 0
 

Sample Output
Case 1: POSSIBLE0 0 0 2 2 21 1 1 3 3 38 8 8 9 9 9Case 2: IMPOSSIBLECase 3: POSSIBLE0 0 0 1 1 1
 

Source
2009 Asia Wuhan Regional Contest Hosted by Wuhan University 

题意:

在三维空间内,有n个长方体,棱都与坐标轴平行。给出一些关系,问是否可能,若可能,输出其中的一种。

关系有两种:

1、I:两个长方体有相交的体积。

2、X或Y或Z:某个长方体的所有点的某一维(X或Y或Z)的坐标完全小于另一个长方体的任意一点。

#include<stdio.h>#include<vector>#include<queue>using namespace std;const int N = 2005;vector<int>mapt[3][N];int in[3][N],v[3][N],n;void init(){    for(int i=0;i<3;i++)        for(int j=1;j<=n*2;j++)        in[i][j]=v[i][j]=0,mapt[i][j].clear();    for(int i=0;i<3;i++)//坐标X,Y,Z        for(int j=1;j<=n;j++)//盒子j,用两个点表示,点j与点j+n        {            in[i][j+n]++; mapt[i][j].push_back(j+n); //点j的坐标比相应的点j+n坐标值小        }}int topeSort(){    for(int i=0;i<3;i++)    {        int k=0;        queue<int>q;        for(int j=1;j<=n;j++)            if(in[i][j]==0)            q.push(j),v[i][j]=k++;        while(!q.empty())        {            int s=q.front(); q.pop();            int len=mapt[i][s].size();            for(int j=0; j<len; j++)            {                int tj=mapt[i][s][j];                in[i][tj]--;                if(v[i][s]+1>v[i][tj])                    v[i][tj]=v[i][s]+1;                if(in[i][tj]==0)                    q.push(tj),k++;            }        }        if(k!=n*2)            return 0;    }    return 1;}int main(){    char ch[5];    int m,a,b,cas=0;    while(scanf("%d%d",&n,&m)>0&&n+m!=0)    {        init();        while(m--)        {            scanf("%s%d%d",ch,&a,&b);            if(ch[0]=='I')            {                for(int i=0;i<3;i++)                {                    mapt[i][a].push_back(b+n); in[i][b+n]++;                    mapt[i][b].push_back(a+n); in[i][a+n]++;                }            }            else if(ch[0]=='X')                mapt[0][a+n].push_back(b),in[0][b]++;            else if(ch[0]=='Y')                mapt[1][a+n].push_back(b),in[1][b]++;            else if(ch[0]=='Z')                mapt[2][a+n].push_back(b),in[2][b]++;        }        printf("Case %d: ",++cas);        if(topeSort()==0)            printf("IMPOSSIBLE\n");        else        {            printf("POSSIBLE\n");            for(int i=1;i<=n;i++)            {                printf("%d",v[0][i]);                for(int j=1;j<3;j++)                    printf(" %d",v[j][i]);                for(int j=0;j<3;j++)                    printf(" %d",v[j][i+n]);                printf("\n");            }        }        printf("\n");    }}


0 0
原创粉丝点击