Balloons in a Box

本文介绍了一种模拟将气球放置于长方体盒子内的算法。该算法通过确定气球的最大体积并计算未被气球占据的空间体积来解决具体问题。输入包括盒子尺寸和可能放置气球的位置坐标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Balloons in a Box


Time Limit: 3000 ms     Case Time Limit: 3000 ms     Memory Limit: 131072 KB
Submit: 6     Accepted: 1 
This problem will be judged on UVALive. Original ID: 2474.

[Prev][Next]

Description


[PDF Link]

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 

Source

World Finals, 2002 - Honolulu
#include<stdio.h>
#include<string.h>
#include<math.h>
const double pi=acos(-1.0);
int n,x1,y11,z1,x2,y2,z2,vis[10],dis[10];
double r[10],vol,max;
int abs(int a)
{
	if(a<0)
		return 0-a;
	return a;
}
struct
{
	int x,y,z;
}p[10];
int min2(int a,int b)
{
	return a<b?a:b;
}
int min3(int a,int b,int c)
{
	return min2(a,min2(b,c));
}
void solve(int k)
{
	int i,j,flag;
	if(k==n)
	{
		if(vol>max)
			max=vol;
	}
	else
		for(i=0;i<n;i++)
			if(!vis[i])
			{
				flag=0;
				r[i]=1000000001;
				for(j=0;j<n;j++)
					if(vis[j])
					{
						double d=sqrt((double)(p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y)+(p[i].z-p[j].z)*(p[i].z-p[j].z))-r[j];
						if(d<0)
						{
							flag=1;
							break;
						}
						if(r[i]>d)
							r[i]=d;
					}
				vis[i]=1;
				if(flag)
					r[i]=0;
				else
					if(r[i]>dis[i])
						r[i]=dis[i];
				vol+=4*pi*r[i]*r[i]*r[i]/3;
				solve(k+1);
				vol-=4*pi*r[i]*r[i]*r[i]/3;
				vis[i]=0;
			}
			
}
int main()
{
	int i,cnt=1;
	while(scanf("%d",&n)&&n)
	{
		scanf("%d%d%d%d%d%d",&x1,&y11,&z1,&x2,&y2,&z2);
		for(i=0;i<n;i++)
			scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z);
		for(i=0;i<n;i++)
			dis[i]=min2(min3(abs(p[i].x-x1),abs(p[i].x-x2),abs(p[i].y-y11)),min3(abs(p[i].y-y2),abs(p[i].z-z1),abs(p[i].z-z2)));
		vol=max=0;
		memset(vis,0,sizeof(vis));
		solve(0);
		printf("Box %d: %.0f\n\n",cnt++,(double)abs(x1-x2)*abs(y11-y2)*abs(z1-z2)-max);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值