盒子里的气球 枚举法 刘汝佳 算法艺术与信息学竞赛 例一 ACM/ICPC World Finals 2002 fzu 1515

Problem 1515 Balloons in a Box

Accept: 137    Submit: 741
Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem Description

You must write a program that simulates placing spherical balloons into a rectangular box.

The simulation scenario is as follows. Imagine that you are given a rectangular box and a set of points inside the box. Each point represents a position where you might place a balloon. To place a balloon at a point, center it at the point and inflate the balloon until it touches a side of the box or a previously placed balloon. You may use the points in any order you like, and need not use every point. Your objective is to place balloons in the box in an order that maximizes the total volume occupied by the balloons.

You are required to calculate the volume within the box that is not enclosed by the balloons.

All integer will be in [-1000, 1000].

Input

The input consists of several test cases. The first line of each test case contains a single integer n that indicates the number of points in the set (n ≤ 6). The second line contains three integers that represent the (x, y, z) integer coordinates of a corner of the box, and the third line contains the (x, y, z) integer coordinates of the opposite corner of the box. The next n lines of the test case contain three integers each, representing the (x, y, z) coordinates of the points in the set. The box has non-zero length in each dimension and its sides are parallel to the coordinate axes.

Output

For each test case print one line which indicates the volume of the box not occupied by balloons. Round the volume to the nearest integer.

Sample Input

2

0 0 0

10 10 10

3 3 3

7 7 7

 

Sample Output

774

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int n;
double v,sv;
struct po
{
    int x, y, z;
    double r;
}p[10],b[2];

bool vis[10];

const double pi = acos(-1.0); //pi=3.141592653
const double inf=10000000000.0;

double min(double a,double b)
{
       if(a<b)
       return a;
       else 
       return b;
}

double volume(po a,po b)//两点之间的距离 
{
     return sqrt((double)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
}

double minst(int i)//找点i上的气球的最大半经 
{
       int j;
       double m=inf;
       double mi[6];
       mi[0]=abs(p[i].x-b[0].x);mi[1]=abs(p[i].y-b[0].y);mi[2]=abs(p[i].z-b[0].z);
       mi[3]=abs(p[i].x-b[1].x);mi[4]=abs(p[i].y-b[1].y);mi[5]=abs(p[i].z-b[1].z);
       for(j=0;j<6;j++ )
       if(mi[j]<m)
         m=mi[j];
      for(j=0;j<n;j++)
      {
           if(i!=j  && vis[j]==true)
           {
               m=min(m,volume(p[i],p[j])-p[j].r);
           }
      }
     // printf("%.2lf\n",m);
      return m;
}

void dfs(int step,double bv)
{
    if(step==n)
    {
        if(bv>v)
        v=bv;
        return ;       
    }
    else
    {
       for(int i=0;i<n;i++)
            if(!vis[i])
            {
                 vis[i]=true;
                 p[i].r=minst(i);
                 if(p[i].r>0)
                   dfs(step+1,bv+(4.0/3.0)*pi*p[i].r*p[i].r*p[i].r);
                 else
                   dfs(step+1,bv);
                 vis[i]=false;
            }
            
    }
    
}

int main()
{
    int i,j;
    while(scanf("%d",&n)!=EOF)
    {
         scanf("%d%d%d%d%d%d",&b[0].x,&b[0].y,&b[0].z,&b[1].x,&b[1].y,&b[1].z);
         sv=fabs(b[0].x-b[1].x)*abs(b[0].y-b[1].y)*abs(b[0].z-b[1].z);//算出长方体的体积 
         for(i=0;i<n;i++)
         {
             scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z);
             p[i].r=0;
         }
         v=0;
         memset(vis,0,sizeof(vis));//用于标记是否遍历过 
         dfs(0,0.0);
         printf("%.lf\n",sv-v);
    }
    return 0;    
}
posted @ 2011-06-01 20:51  tonyspace  阅读(959)  评论(0编辑  收藏  举报