UVA1009 Balloons in a Box 枚举

黑书上面的第一道例题,注意数据的类型之间的关系。 另外注意每个答案后面都要跟一个空行。

利用深搜进行全排列的枚举,得到相应的放置顺序,再求出每个点最小的半径(需要根据点到矩形的六个面的距离与点到之前所有已经膨胀好的气球的距离比较得到最小值),接着计算出所有气球都膨胀完后的体积。

得到最大的体积之后,与矩形体积相减便可以得到要求的最小剩余体积。

 

。。。感觉自己写的max 和 min函数很不靠谱,数据类型有点乱,以后还是直接调用库函数算了。

 

Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

[]   [Go Back]   [Status]  

Description

Download as PDF

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.

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 (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.

Output

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 

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

double x[15],y[15],z[15],r[15];
double X1,Y1,Z1,X2,Y2,Z2,maxV;
int n,sel[15];
bool vis[15];
const double pi=acos(-1.0);


double getside(int i)
{
    double dis1,dis2,dis3;
    dis1=min(fabs(x[i]-X1),fabs(x[i]-X2));
    dis2=min(fabs(y[i]-Y1),fabs(y[i]-Y2));
    dis3=min(fabs(z[i]-Z1),fabs(z[i]-Z2));
    return min(dis1,min(dis2,dis3));
}

double distances(int a,int b)
{
    return sqrt((x[a]-x[b])*(x[a]-x[b])+
                (y[a]-y[b])*(y[a]-y[b])+
                (z[a]-z[b])*(z[a]-z[b]));
}

void getV()
{
    double a,v=0;
    for(int i=0;i<n;i++)
    {
        a=getside(sel[i]);
        for(int j=0;j<i;j++)
            a=min(a,distances(sel[i],sel[j])-r[sel[j]]);
        r[sel[i]]=(a<0)?0:a;
        if(a<=0) continue;
        v+=(4.0/3*pi*r[sel[i]]*r[sel[i]]*r[sel[i]]);
    }
    maxV=max(maxV,v);
}

void dfs(int cnt)
{
    if(cnt==n)
        getV();
    else
    {
        for(int i=0;i<n;i++)
        {
            if(vis[i]) continue;
            sel[cnt]=i;
            vis[i]=1;
            dfs(cnt+1);
            vis[i]=0;
        }
    }
}

int main()
{
    int te=1;
    while(~scanf("%d",&n)&&n)
    {
        scanf("%lf%lf%lf",&X1,&Y1,&Z1);
        scanf("%lf%lf%lf",&X2,&Y2,&Z2);
        for(int i=0;i<n;i++)
            scanf("%lf%lf%lf",&x[i],&y[i],&z[i]);
        maxV=0;
        memset(vis,0,sizeof(vis));
        dfs(0);
        printf("Box %d: %.0lf\n\n",te++,((fabs((X1-X2)*(Y1-Y2)*(Z1-Z2)))-maxV));

    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值