天天看点

hdu 3231 Box Relations Box Relations

Box Relations

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 828    Accepted Submission(s): 295

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  

Recommend chenrui  

================================================================================================== 转化为拓扑排序

#include
#include
using namespace std;

const int N=2010;
bool g[3][N][N];
int ind[3][N],n;
int topo[3][N];
int x[N],y[N],z[N];
bool toposort(int k)
{
   memset(ind[k],0,sizeof(ind[k]));
    for(int v=1;v<=n; v++)
       for(int u=1; u<=n; u++)
          if(g[k][u][v]) ind[k][v]++;
    for(int i=0; i
    {
       int u;
       for(u=1; u<=n; u++)
          if(ind[k][u]==0) break;
       if(u>n) return false;
       topo[k][i]=u;
       ind[k][u]--;
       for(int v=1; v<=n; v++)
          if(g[k][u][v]) ind[k][v]--;
    }
    return true;
}
int main()
{
    int m,C=0;
   while(scanf("%d%d",&n,&m),n||m)
    {
       memset(g,0,sizeof(g));
       for(int i=0; i<3; i++)
           for(intu=1; u<=n; u++) g[i][u][u+n]=1;
       while(m--)
       {
           charop[2];
           intu,v;
          scanf("%s%d%d",op,&u,&v);
          if(op[0]=='I')
           {
              for(int i=0;i<3; i++)
                 g[i][v][u+n]=g[i][u][v+n]=1;
           }
           else
           {
              int k=op[0]-'X';
              g[k][u+n][v]=1;
           }
       }
       n*=2;
       bool flag=true;
       for(int i=0;flag&&i<3;i++)
          if(!toposort(i)) flag=false;
       n/=2;
       if(flag)
       {
          printf("Case %d: POSSIBLE\n",++C);
           for(inti=0; i<2*n; i++)
             x[topo[0][i]]=y[topo[1][i]]=z[topo[2][i]]=i;
           for(inti=1; i<=n; i++)
              printf("%d %d %d %d %d%d\n",x[i],y[i],z[i],x[i+n],y[i+n],z[i+n]);
       }
       else printf("Case %d: IMPOSSIBLE\n",++C);
       printf("\n");
    }
    return 0;
}
           

继续阅读