【枚举】Balloons in a Box 【黑书-UVa1009】

题目链接

题目描述:

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. 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 not use a point that is outside the box or inside a previously placed balloon. However, you may use the points in any order you like, and you 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.

输入:

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 (1 ≤ 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. The input is terminated by the number zero on a line by itself.

输出:

For each test case print one line of output consisting of the test case number followed by the volume of the box not occupied by balloons. Round the volume to the nearest integer. Follow the format in the sample output given below. Place a blank line after the output of each test case.

Sample Input

2

0 0 0

10 10 10

3 3 3

7 7 7

0

Sample Output

Box 1: 774


【小结】:

下次如果遇到double 精度问题,千万不要去用if判断,下次直接用max( ) 不然一直陷入WA无法自拔。

【题解】:

这个题目就是根据书本上的做法做的,我们需要枚举每一个情况,那就是6!=720种情况,

要注意立体几何需要注意,不能和长方体的 面接触,

还有空间中两点间距离公式:

#include<bits/stdc++.h>
using namespace std;
const double inf=10000000;
const double pi=acos(-1);
const double eps=1e-27;
typedef struct point{
    double x,y,z;
}p;
p o1,o2,a[10];
double V,R[10];
int cir[6]={0,1,2,3,4,5},n;
void getR(p t,int No){

    double  L,H,W;
    L=min( fabs(o1.x-t.x) , fabs(o2.x-t.x) );
    W=min( fabs(o1.y-t.y) , fabs(o2.y-t.y) );
    H=min( fabs(o1.z-t.z) , fabs(o2.z-t.z) );
    //printf("%f %f %f \n",L,W,H);
    double D=inf,tmp=0,tx,ty,tz;
    if(No==0){
        R[cir[0]]=min(L,min(W,H));
        return ;
    }
    for(int i=No-1;i>=0;i--){
        tx=(a[cir[i]].x-a[cir[No]].x)*(a[cir[i]].x-a[cir[No]].x);
        ty=(a[cir[i]].y-a[cir[No]].y)*(a[cir[i]].y-a[cir[No]].y);
        tz=(a[cir[i]].z-a[cir[No]].z)*(a[cir[i]].z-a[cir[No]].z);
        if(R[cir[i]]==0){
            continue;
        }
        /*if(fabs(tmp) >=eps  ){
            tmp=sqrt(tx+ty+tz)-R[cir[i]];
        }else{
            tmp=0;
        }*/
        tmp=max(sqrt(tx+ty+tz)-R[cir[i]],0.0);
        D=min(D,tmp);
    }
    R[cir[No]]=min(min(L,H),min(W,D));
}
int main()
{
    int kase=1;
    while(~scanf("%d",&n)&&n){
        scanf("%lf%lf%lf",&o1.x,&o1.y,&o1.z);
        scanf("%lf%lf%lf",&o2.x,&o2.y,&o2.z);
        V=fabs(o1.x-o2.x)*fabs(o1.y-o2.y)*fabs(o1.z-o2.z);
        //cout<<"V  =  "<<V<<endl;
        for(int i=0;i<n;i++){
            scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].z);
        }
        for(int i=0;i<6;i++){
            cir[i]=i;
        }
        double ans=V;
        do{
            for(int i=0;i<n;i++){
                R[cir[i]]=0;
            }
            for(int i=0;i<n;i++){
                getR(a[cir[i]],i);
            }
            double tV=0;
            for(int i=0;i<n;i++){
                //printf(\\"R[cir[%d]] =  %f\n",i,R[cir[i]]);
                tV=tV+(4.0*pi*R[cir[i]]*R[cir[i]]*R[cir[i]])/3.0;
            }
            ans=min(ans,V-tV);
        }while(next_permutation(cir,cir+n));

        printf("Box %d: %.0f\n",kase++,ans);
        printf("\n");
    }
    return 0;
}
/*
2
0 0 0
10 10 10
3 3 3
7 7 7

4
73 256 -38 -175 944 136
-37 332 1
-63 743 64
48 472 -32
35 753 38

6
808 877 -957 661 329 -738
763 572 -792
687 539 -879
673 795 -811
679 500 -783
717 545 -883
667 629 -825

774
27763968
16491870
*/

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值