HDOJ 3231 Box Relations 拓扑排序

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;
}


1、资源项目源码均已通过严格测试验证,保证能够正常运行;、 2项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行;、 2项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 、 1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READmE.文件(md如有),本项目仅用作交流学习参考,请切勿用于商业用途。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值