《算法艺术与信息学竞赛》之 枚举 例一 Balloons in a Box

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<bits/stdc++.h>
const double pi=acos(-1.0);//反三角函数,π的精度较高,也可以赋为 3.14159265358979323846 
using namespace std;
double d[10],p[10][3],dis[10][10],r[10],sum,maxs;
int n;
bool vis[10]; 
void dfs(int k){
    if(k==n)
        maxs=max(maxs,sum);//maxs存储最大半径三次方和 
    else
    for(int i=0;i<n;i++){
        if(vis[i]==0){//未访问 
            r[i]=d[i];//dis[i]存的是第i个球到边界的最短距离,赋给r[i]在与其他球比较 
            for(int j=0;j<n;j++){
                if(vis[j]==1){
                    if(dis[i][j]>r[j])
                        r[i]=min(r[i],dis[i][j]-r[j]);//去最小的一个作为最大半径 
                    else r[i]=0;//已经被别的球覆盖了,不能有气球 
                }
            }
            sum+=r[i]*r[i]*r[i];
            vis[i]=1;
            dfs(k+1);
            sum-=r[i]*r[i]*r[i];//dfs还原现场 
            vis[i]=0;
        }
    }
}
int main(){
    int k,m,v,K=0;;
    while(cin>>n&&n){
        int x1,x2,y1,y2,z1,z2;
        scanf("%d%d%d%d%d%d",&x1,&y1,&z1,&x2,&y2,&z2);
        v=abs(x1-x2)*abs(y1-y2)*abs(z1-z2); 
        for(int i=0;i<n;i++){
            cin>>p[i][0]>>p[i][1]>>p[i][2];
            d[i]=min(fabs(p[i][0]-x1),fabs(p[i][0]-x2));
            d[i]=min(d[i],min(fabs(p[i][1]-y1),fabs(p[i][1]-y2)));
            d[i]=min(d[i],min(fabs(p[i][2]-z1),fabs(p[i][2]-z2)));//求出最短的距边界距离 
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++)
                dis[i][j]=sqrt((p[i][0]-p[j][0])*(p[i][0]-p[j][0])+(p[i][1]-p[j][1])*(p[i][1]-p[j][1])+(p[i][2]-p[j][2])*(p[i][2]-p[j][2]));
        }
        maxs=0;
        memset(vis,0,sizeof(vis));
        dfs(0);
        printf("Box %d: %.0lf\n\n",++K,(v-(double)4.0/3*pi*maxs));
    }
    return 0;
}

看代码应该很容易懂,就不再多做解释。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值