现在的位置: 首页 > 综合 > 正文

HDOJ 3231 Box Relations 拓扑排序

2018年05月02日 ⁄ 综合 ⁄ 共 3375字 ⁄ 字号 评论关闭

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 the
x, 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 from
j):

  • 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 smaller
    x-coordinate than any point inside Cj.
  • Y i j: The intersection volume is zero, and any point inside Ci has smaller
    y-coordinate than any point inside Cj.
  • Z i j: The intersection volume is zero, and any point inside Ci has smaller
    z-coordinate than any point inside Cj.

.

 

Input
There will be at most 30 test cases. Each case begins with a line containing two integers
n (1 <= n <= 1,000) and R (0 <= R <= 100,000), the number of boxes and the number of relations. Each of the following
R lines describes a relation, written in the format above. The last test case is followed by
n=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, the
i-th line of the following n lines contains six integers x1, y1, z1, x2, y2, z2, that means the
i-th box is the set of points (x,y,z) satisfying x1 <= x <= x2, y1 <= y <= y2, z1 <= z <= z2. The absolute values of
x1, 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 2 I 1 2 X 2 3 3 3 Z 1 2 Z 2 3 Z 3 1 1 0 0 0
 

Sample Output
Case 1: POSSIBLE 0 0 0 2 2 2 1 1 1 3 3 3 8 8 8 9 9 9 Case 2: IMPOSSIBLE Case 3: POSSIBLE 0 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;
}

抱歉!评论已关闭.