hdu Box Relations

Box Relations

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 235    Accepted Submission(s): 92
 
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
 
Recommend
chenrui

分析:该题为Special Judge。按照给出的条件对x,y,z三个方向分别拓扑排序即可。

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;

typedef struct S {
    int val;
    struct S *next;
} EDGE;
EDGE *e[3][2010], temp[1000010];
bool vis[3][2010][2010];
int cnt, deg[3][2010], ok;
int n, x[3][2010], k[3];

void init() {
    int i;
    cnt = 0;
    ok = 1;
    memset(deg, 0, sizeof (deg));
    for (i = 1; i <= n * 2; ++i) {
        e[0][i] = e[1][i] = e[2][i] = NULL;
    }
    memset(vis, 0, sizeof (vis));
    k[0] = k[1] = k[2] = 0;
}

void add(int type, int x, int y) {
    if (vis[type][x][y])
        return;
    temp[cnt].val = y;
    temp[cnt].next = e[type][x];
    e[type][x] = &temp[cnt];
    cnt++;
    deg[type][y]++;
    vis[type][x][y] = 1;
}

int main() {
    int m, a, y, b, i, j, t, T = 0;
    char s[2];
    while (scanf("%d%d", &n, &m) != EOF && n + m) {
        init();
        for (i = 1; i <= n; ++i) {
            add(0, i, i + n);
            add(1, i, i + n);
            add(2, i, i + n);
        }
        while (m--) {
            scanf("%s%d%d", s, &a, &b);
            if (s[0] == 'I') {
                add(0, a, b + n);
                add(0, b, a + n);
                add(1, a, b + n);
                add(1, b, a + n);
                add(2, a, b + n);
                add(2, b, a + n);
            } else if (s[0] == 'X') {
                add(0, a + n, b);
            } else if (s[0] == 'Y') {
                add(1, a + n, b);
            } else if (s[0] == 'Z') {
                add(2, a + n, b);
            }
        }
        if (!ok)
            goto L;
        for (i = 0; i < 3; ++i) {
            queue <int> q;
            for (j = 1; j <= 2 * n; ++j) {
                //      printf("i=%d j=%d deg=%d\n",i,j,deg[i][j]);
                if (!deg[i][j])
                    q.push(j);
            }
            while (!q.empty()) {
                t = q.front();
                q.pop();
                x[i][t] = k[i]++;
                for (; e[i][t]; e[i][t] = e[i][t]->next) {
                    y = e[i][t]->val;
                    if (--deg[i][y] == 0)
                        q.push(y);
                }
            }
            //      printf("i=%d k[i]=%d\n", i, k[i]);
            if (k[i] < 2 * n) {
                ok = 0;
                break;
            }
        }
L:
        printf("Case %d: ", ++T);
        if (!ok)
            printf("IMPOSSIBLE\n");
        else {
            printf("POSSIBLE\n");
            for (i = 1; i <= n; ++i)
                printf("%d %d %d %d %d %d\n", x[0][i], x[1][i], x[2][i], x[0][i + n], x[1][i + n], x[2][i + n]);
        }
        printf("\n");
    }
    return 0;
}

 

posted @ 2012-09-16 00:30  YogyKwan  阅读(204)  评论(0编辑  收藏  举报