黑书上面的第一道例题,注意数据的类型之间的关系。 另外注意每个答案后面都要跟一个空行。
利用深搜进行全排列的枚举,得到相应的放置顺序,再求出每个点最小的半径(需要根据点到矩形的六个面的距离与点到之前所有已经膨胀好的气球的距离比较得到最小值),接着计算出所有气球都膨胀完后的体积。
得到最大的体积之后,与矩形体积相减便可以得到要求的最小剩余体积。
。。。感觉自己写的max 和 min函数很不靠谱,数据类型有点乱,以后还是直接调用库函数算了。
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
![Download as PDF Download as PDF](https://i-blog.csdnimg.cn/blog_migrate/2ea975a8d421ce3226c46b0946fd5932.png)
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;
}