Box Relations(拓扑排序)

Box Relations
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 510 Accepted Submission(s): 217

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个长方形 在三维坐标中 他们只有这两种情况
相交 或者 某个长方体的任意一轴 的长度 都比另外一个长方体的长度要小
这是我参考的博客:http://blog.csdn.net/dgq8211/article/details/8038993 他写的挺好的 就是代码没有注释

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<vector>
#include<queue>

using namespace std;

struct T
{
    int v,next;
}E[3][2005*100];
struct M
{
    int head;//前驱
    int rd;//入度
    int dep;//某一轴的下标
}V[3][2005];
int top[3],n;

void Add_Edge(int k,int u,int v)
{
    E[k][top[k]].v = v;//更新边
    E[k][top[k]].next = V[k][u].head;//更新前驱关系 以及入度的值
    V[k][u].head = top[k]++;
    ++V[k][v].rd;
}
bool Top_Sort(int k)//拓扑排序
{
    queue<int> Q;
    for(int i=1;i<=n;i++)
        if(V[k][i].rd == 0)//寻找入度为0的点 即根节点
            Q.push(i);
    int cnt = 0;
    while(!Q.empty())
    {
        ++cnt;
        int p = Q.front();
        for(int i=V[k][p].head;i!=NULL;i=E[k][i].next)//遍历与顶点p相邻的所有顶点
        {
            int q = E[k][i].v;//删除点p 并将与点p相邻的所有点的入度减一
            --V[k][q].rd;
            if(V[k][q].rd == 0)//发现此事入度为0的点就放入队列里面
            {
                Q.push(q);
                V[k][q].dep = V[k][p].dep + 1;//第k维的坐标加1
            }
        }
        Q.pop();
    }
    return cnt == n;//如果cnt==n的值则说明2*nn个点全部找到了
}
int main()
{
    int u,v,nn,ncase=0,m;
    char cmd;
    while(~scanf("%d%d%*c",&nn,&m),nn)
    {
        memset(V,0,sizeof(V));//初始化
        top[0] = top[1] = top[2] = 1;
        n = 2*nn;
        for(int k=0;k<3;k++)
            for(int i=1;i<=nn;i++)
                Add_Edge(k,i,i+nn);//保证另外一个任意轴都比另外一个长方体的相应的轴坐标都小
        while(m--)
        {
            scanf("%c%d%d%*c",&cmd,&u,&v);
            if(cmd == 'I')
            {
                for(int k=0;k<3;k++)
                {
                    Add_Edge(k,u,v+nn);//这里说明了 如果两个长方体相交 则说明左边的长方体的任意一轴小于另外一轴的坐标
                    Add_Edge(k,v,u+nn);
                }
            }
            else
                Add_Edge(cmd-'X',u+nn,v);
        }
        printf("Case %d: ",++ncase);
        if(!Top_Sort(0) || !Top_Sort(1) || !Top_Sort(2))
            puts("IMPOSSIBLE\n");
        else
        {
            puts("POSSIBLE");
            for(int i=1;i<=nn;i++)
                printf("%d %d %d %d %d %d\n",V[0][i].dep,V[1][i].dep,V[2][i].dep,V[0][i+nn].dep,V[1][i+nn].dep,V[2][i+nn].dep);
            puts("");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值