HDU3231 Box Relations(拓扑排序)经典

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

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");
    }
}
时间: 2024-10-07 22:06:41

HDU3231 Box Relations(拓扑排序)经典的相关文章

HDU3231 Box Relations——三维拓扑刨铣

HDU3231 Box Relations 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3231 题目意思:在一个三维空间上有一些棱和坐标轴平行的立方体.给够给予四种关系.I i j表示i,j两个立方体是有部分的重合,X i j表示立方体i所有点x坐标都小于立方体j的x坐标,Y i j表示立方体i所有点y坐标都小于立方体j的y坐标,Z i j表示立方体i所有点Z坐标都小于立方体j的Z坐标.然后给出一系列这样的关系,问你这些关系是否存在矛盾,如果不

【Vj作业】【拓扑排序经典理解题】Ordering Tasks 1、Kahn算法;2、基于DFS的算法。

2018-02-13 链接   https://cn.vjudge.net/contest/211129#problem/D John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is only possible if other tasks have already been executed.InputThe input will consist o

ZOJ3524Crazy Shopping(完全背包+拓扑排序)经典

H - Crazy Shopping Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description Because of the 90th anniversary of the Coherent & Cute Patchouli (C.C.P), Kawashiro Nitori decides to buy a lot of rare things

poj 2585 Window Pains(拓扑排序)(经典)(困难)

Window Pains Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1756   Accepted: 881 Description Boudreaux likes to multitask, especially when it comes to using his computer. Never satisfied with just running one application at a time, he u

HDU 3213 Box Relations(拓扑排序构造)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3231 题意:有n个长方体,四种限制条件.(1)I x y x和y有相交:(2)X/Y/Z  x y x的最大X/Y/Z坐标小于y的最大X/Y/Z.构造出这样的n个长方体. 思路:首先,XYZ三个方向是可以分开考 虑的.那么我们可以一个个分别求解.将每个长方体拆成左上角右下角两个点,我们假设现在考虑X方向,也即是一个长方体对应两个X方向的点,共2*n个点, 边<i,j>表示i小于j,那么首先有边&l

Hdu 3231 Box Relations(拓扑排序)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3231 思路:拓扑排序.将每个长方体的每对面看成一条线段,将线段看成两个点,则共有3条线段,6个点. 对于一组相交关系,若两个长方体相交,当且仅当每一维中一个长方体的面插入另一个长方体的内部. 将长方体分为左右,上下,前后三维. 例如一长方形设左面为x,右面为x+n,另一长方体左面为y,右面为y+n,则在该维中坐标x<y+n,y<x+n,x<x+n,其他两维同理. 则由大小关系,分别对每一维拓

HDU3231拓扑排序

Box Relations Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1334    Accepted Submission(s): 540Special Judge Problem Description There are n boxes C1, C2, ..., Cn in 3D space. The edges of the

【DFS】【拓扑排序】【动态规划】Gym - 100642A - Babs&#39; Box Boutique

给你10个箱子,有长宽高,每个箱子你可以决定哪个面朝上摆.把它们摞在一起,边必须平行,上面的不能突出来,问你最多摆几个箱子. 3^10枚举箱子用哪个面.然后按长为第一关键字,宽为第二关键字,从大到小排序. 如果前面的宽大于等于后面的宽,就连接一条边. 形成一张DAG,拓扑排序后跑最长路即可. #include<cstdio> #include<cstring> #include<queue> #include<algorithm> using namespa

UVA Ordering Tasks (经典拓扑排序)

   题意:n个任务,m组数据,每组数据输入x,y代表如果想要完成y任务需要先完成x任务,最后输出任务的完成顺序. 经典的拓扑排序. 代码: #include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> using namespace std; int map[105][105