hdu 3231 Box Relations (拓扑排序)

Box Relations

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

 

Recommend
chenrui   |   We have carefully selected several similar problems for you:  3236 3234 3237 3232 3233 
 
 1 //140MS    1276K    1924 B    G++
 2 /*
 3 
 4     题意:
 5         给出n个矩阵的一系列的关系,输出满足关系的n个矩阵的对角坐标。
 6     
 7     拓扑排序:
 8         矩阵就有六个面,x、y、z各有两个面,设上表面为i+n,下表面为i,
 9     然后每次的XYZ操作就是对x、y、z轴的上下表面进行拓扑排序,其中要
10     注意的一点就是I A B操作,要求A的上表面大于B的下表面,B的上表面
11     要大于A的下表面。 调试了挺久的。 
12 
13 */
14 #include<iostream>
15 #include<vector>
16 #include<queue>
17 #define N 2005
18 using namespace std;
19 int in[3][N];
20 vector<int>V[3][N];
21 int n,r,cas;
22 void init()  //初始化 
23 {
24     memset(in,0,sizeof(in));
25     for(int ii=0;ii<3;ii++)
26         for(int i=0;i<=2*n;i++){
27             V[ii][i].clear();
28         }    
29     for(int ii=0;ii<3;ii++)
30         for(int i=1;i<=n;i++){
31             V[ii][i].push_back(i+n);
32             in[ii][i+n]++;
33         }
34 }
35 int topo(int x[],int id) //topo排序 
36 {
37     queue<int>Q;
38     int ii=0;
39     for(int i=1;i<=2*n;i++)
40         if(in[id][i]==0)
41             Q.push(i);
42     //printf("*%d\n",Q.size());
43     while(!Q.empty()){
44         int t=Q.front();
45         Q.pop();
46         x[t]=++ii; //出来的先排 
47         for(int i=0;i<V[id][t].size();i++){
48             if(--in[id][V[id][t][i]]==0)
49                 Q.push(V[id][t][i]);
50         }
51     }
52     //printf("**%d\n",ii);
53     if(ii!=2*n) return 1;
54     return 0;
55 } 
56 void solve()
57 {
58     int a[3][N]={0}; //记录答案 
59     int flag=0;
60     for(int i=0;i<3;i++)
61         if(topo(a[i],i)) flag=1;
62     if(flag) printf("Case %d: IMPOSSIBLE\n\n",cas++);
63     else{
64         printf("Case %d: POSSIBLE\n",cas++);
65         for(int i=1;i<=n;i++)
66             printf("%d %d %d %d %d %d\n",a[0][i],a[1][i],a[2][i],a[0][i+n],a[1][i+n],a[2][i+n]);
67         printf("\n");
68     }
69 }
70 int main(void)
71 {
72     int a,b;
73     char op;
74     cas=1;
75     while(scanf("%d%d%*c",&n,&r),n+r)
76     {
77         init();
78         for(int i=0;i<r;i++){
79             scanf("%c%d%d%*c",&op,&a,&b);
80             if(op=='I'){
81                 for(int ii=0;ii<3;ii++){
82                     V[ii][a].push_back(b+n);
83                     in[ii][b+n]++;
84                     V[ii][b].push_back(a+n);
85                     in[ii][a+n]++;
86                 }
87             }else{
88                 V[op-'X'][a+n].push_back(b);
89                 in[op-'X'][b]++;
90             }
91         }
92         solve();
93     }
94     return 0;
95 }

 

posted @ 2014-04-10 11:44  heaventouch  阅读(334)  评论(0编辑  收藏  举报