HDOJ 3231 Box Relations 拓扑排序

来源:互联网 发布:微盘源码 编辑:程序博客网 时间:2024/05/22 06:23

Box Relations

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 996    Accepted Submission(s): 367
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

/*不会啊!! 给出n个矩阵的一系列的关系,输出满足关系的n个矩阵的对角坐标。拓扑排序:把一个箱子分成三个面,即X,Y,Z拿X来说,把这个面分为上下两部分,上面记为1,下面记为1+n,当读入X A B条件的时候,那么A箱的下表面和B箱的上表面构成关系,A要比B小I A B操作,就是相交,要求A的上表面大于B的下表面,B的上表面要大于A的下表面。 *//*分别对x,y,z topsort就ok了注意有公共部分的时候的建边*/#include <iostream>#include <cstdio>#include <cstdlib>#include <string>#include <cstring>#include <map>#include <vector>#include <cmath>#include<queue>using namespace std;int n,r;char s[3];int x,y;int inx[1009*2];int iny[1009*2];int inz[1009*2];int ansx[1009*2],ansy[1009*2],ansz[1009*2];vector<int> vx[1009*2],vy[1009*2],vz[1009*2];void init(){    memset(inx,0,sizeof(inx));    memset(iny,0,sizeof(iny));    memset(inz,0,sizeof(inz));    for(int i=1;i<=2*n;i++)    {        vx[i].clear();        vy[i].clear();        vz[i].clear();    }}bool solve(int *inx,vector<int> vx[],int ansx[]){    queue<int> q;    int num=0;    for(int i=1;i<=2*n;i++)    if(inx[i]==0)    {        q.push(i);    }    while(!q.empty())    {        int x=q.front();q.pop();        ansx[x]=++num;        for(int i=0;i<vx[x].size();i++)        {            int f=vx[x][i];            inx[f]--;            if(inx[f]==0)            {                q.push(f);           }        }    }    if(num==2*n)return 1;    return 0;}int main(){    int ca=1;   // freopen("test.txt","r",stdin);    while(scanf("%d%d",&n,&r),n+r)    {        init();        for(int i=1;i<=n;i++)        {            vx[i].push_back(i+n);            inx[i+n]++;            vy[i].push_back(i+n);            iny[i+n]++;            vz[i].push_back(i+n);            inz[i+n]++;        }        for(int i=0;i<r;i++)        {            scanf("%s%d%d",s,&x,&y);            if(s[0]=='I')            {               vx[x].push_back(y+n);               vx[y].push_back(x+n);               inx[y+n]++;               inx[x+n]++;               vy[x].push_back(y+n);               vy[y].push_back(x+n);               iny[y+n]++;               iny[x+n]++;               vz[x].push_back(y+n);               vz[y].push_back(x+n);               inz[y+n]++;               inz[x+n]++;            }            else if(s[0]=='X')            {                inx[y]++;                vx[x+n].push_back(y);            }            else if(s[0]=='Y')            {                iny[y]++;                vy[x+n].push_back(y);            }            else if(s[0]=='Z')            {                inz[y]++;                vz[x+n].push_back(y);            }        }        int flag=1;        if(!solve(inx,vx,ansx)){flag=0;}        if(!solve(iny,vy,ansy)){flag=0;}        if(!solve(inz,vz,ansz)){flag=0;}        printf("Case %d: ",ca++);        if(!flag){puts("IMPOSSIBLE");puts("");continue;}        {            puts("POSSIBLE");            for(int i=1;i<=n;i++)            {                printf("%d %d %d %d %d %d\n",ansx[i],ansy[i],ansz[i],ansx[i+n],ansy[i+n],ansz[i+n]);            }            puts("");        }    }    return 0;}


0 0
原创粉丝点击