UVA 2474 - Balloons in a Box 爆搜
題目連接:
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=99999999&page=show_problem&category=36&problem=475&mosmsg=Submission+received+with+ID+1967446
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. 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
Hint
題意
將n個(gè)氣球放到一個(gè)長(zhǎng)方體盒子里面, 然后氣球會(huì)一直膨脹, 直到碰到盒壁或者其他氣球. 先要求你找到一個(gè)放氣球的順序, 使得最后最后氣球占據(jù)的體積最大.
數(shù)據(jù)規(guī)模: 1≤n≤6
題解:
直接爆搜就好了,數(shù)據(jù)范圍很小……
代碼
#include<bits/stdc++.h> using namespace std; const int maxn = 10; const double eps = 1e-8; const double pi = acos(-1.0); double sqr(double x) {return x*x; } double dis(double x1,double y11,double z1,double x2,double y2,double z2) {return sqrt(sqr(x1-x2)+sqr(y11-y2)+sqr(z1-z2)); } int n; double x[maxn],y[maxn],z[maxn],ans,r[maxn]; int vis[maxn],u[maxn],cas; double x1,y11,z1,x2,y2,z2,a,b,c; void QAQ() {memset(vis,0,sizeof(vis)); } void dfs(int cnt,double v) {if(cnt>=n){ans=min(ans,v);return;}for(int i=0;i<n;i++){if(vis[i])continue;vis[i]=1,u[cnt]=i;r[cnt]=min(x[i],a-x[i]);r[cnt]=min(r[cnt],min(y[i],b-y[i]));r[cnt]=min(r[cnt],min(z[i],c-z[i]));for(int j=0;j<cnt;j++){double d = dis(x[i],y[i],z[i],x[u[j]],y[u[j]],z[u[j]]);r[cnt]=min(r[cnt],d-r[j]);}r[cnt]=max(r[cnt],0.0);dfs(cnt+1,v-4.0/3.0*pi*r[cnt]*r[cnt]*r[cnt]);vis[i]=0;} } void TAT() {cas++;scanf("%lf%lf%lf%lf%lf%lf",&x1,&y11,&z1,&x2,&y2,&z2);a=fabs(x1-x2),b=fabs(y11-y2),c=fabs(z1-z2);x1=min(x1,x2),y11=min(y11,y2),z1=min(z1,z2);for(int i=0;i<n;i++){scanf("%lf%lf%lf",&x[i],&y[i],&z[i]);x[i]-=x1,y[i]-=y11,z[i]-=z1;}ans=a*b*c;dfs(0,a*b*c);printf("Box %d: %.0f\n\n",cas,round(ans+eps)); } int main() {while(scanf("%d",&n)!=EOF){if(n==0)break;QAQ();TAT();} }轉(zhuǎn)載于:https://www.cnblogs.com/qscqesze/p/5567636.html
總結(jié)
以上是生活随笔為你收集整理的UVA 2474 - Balloons in a Box 爆搜的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 在线支付模块小结
- 下一篇: [Training Video - 1]