uva 1009 - 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

先贴出来还没搞明白在福州大学上为什么不能AC!!!

代码:

#include <iostream>
#include <cstring>
#include <iomanip>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <map>
#include <cmath> 
const double pi=acos(-1.0); 
using namespace std;
 double dist[10];
 double p[10][3];
 double boll[2][3];
 double pdist[10][10];
 double pr[10];
 double sum,maxs;
 int bis[10],n;
void cmp()
{
	int i,j;
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		pdist[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]));
	}
}
void dfs(int k)
{
	if(k==n) 
	{
		maxs=max(maxs,sum);
	}
	else 
	for(int i=0;i<n;i++)
	{
		if(bis[i]==0)
		{
			pr[i]=dist[i];
			for(int j=0;j<n;j++)
			{
				if(bis[j]==1)
				{
					if(pdist[i][j]>pr[j])
					{
						pr[i]=min(pr[i],pdist[i][j]-pr[j]);
					}
				    else pr[i]=0;
				}
			}
			sum+=pr[i]*pr[i]*pr[i];
			bis[i]=1;
			dfs(k+1);
			sum-=pr[i]*pr[i]*pr[i];
			bis[i]=0;
		} 
	}
}
int main()
{
	int i,j,k,m,v,K=0;;
	while(cin>>n&&n)
	{
		for(i=0;i<2;i++)
		   cin>>boll[i][0]>>boll[i][1]>>boll[i][2];
		v=fabs(boll[0][0]-boll[1][0])*fabs(boll[0][1]-boll[1][1])*fabs(boll[0][2]-boll[1][2]);
		for(i=0;i<n;i++)
		{
			cin>>p[i][0]>>p[i][1]>>p[i][2];
			dist[i]=min(fabs(p[i][0]-boll[0][0]),fabs(p[i][0]-boll[1][0]));
			dist[i]=min(dist[i],min(fabs(p[i][1]-boll[0][1]),fabs(p[i][1]-boll[1][1])));
			dist[i]=min(dist[i],min(fabs(p[i][2]-boll[0][2]),fabs(p[i][2]-boll[1][2])));
			
		}
		cmp();
		maxs=0;memset(bis,0,sizeof(bis));
	    dfs(0); 
		double ss;
		printf("Box %d: %.0lf\n\n",++K,(v-(double)4.0/3*pi*maxs));
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Based on the given specifications, here is a sample implementation in Java using JavaFX library: ```java import javafx.animation.AnimationTimer; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.stage.Stage; import java.util.Random; public class BalloonGame extends Application { private static final int WINDOW_WIDTH = 500; private static final int WINDOW_HEIGHT = 500; private static final Color BACKGROUND_COLOR = Color.BLACK; private static final int MIN_BALLOON_RADIUS = 20; private static final int MAX_BALLOON_RADIUS = 40; private static final double BALLOON_APPEAR_TIME = 2.0; private static final double INITIAL_SPEED = 1.0; private static final int MAX_Y_VELOCITY = 100; private static final Random RANDOM = new Random(); private Pane gamePane; private double lastBalloonTime = 0.0; private double speed = INITIAL_SPEED; @Override public void start(Stage primaryStage) throws Exception { gamePane = new Pane(); gamePane.setPrefSize(WINDOW_WIDTH, WINDOW_HEIGHT); gamePane.setStyle("-fx-background-color: black;"); AnimationTimer gameLoop = new AnimationTimer() { @Override public void handle(long now) { update(now); } }; gameLoop.start(); Scene scene = new Scene(gamePane, WINDOW_WIDTH, WINDOW_HEIGHT); primaryStage.setScene(scene); primaryStage.show(); } private void update(long now) { double elapsedTime = (now - lastBalloonTime) / 1_000_000_000.0; if (elapsedTime >= BALLOON_APPEAR_TIME / speed) { createBalloon(); lastBalloonTime = now; } for (Circle balloon : gamePane.getChildren()) { double yVelocity = MAX_Y_VELOCITY + speed * 50; double y = balloon.getCenterY() + yVelocity * elapsedTime; balloon.setCenterY(y); if (y - balloon.getRadius() > WINDOW_HEIGHT) { gamePane.getChildren().remove(balloon); } } speed = (now / 1_000_000_000.0) / 10.0 + INITIAL_SPEED; } private void createBalloon() { int radius = RANDOM.nextInt(MAX_BALLOON_RADIUS - MIN_BALLOON_RADIUS + 1) + MIN_BALLOON_RADIUS; double x = RANDOM.nextInt(WINDOW_WIDTH - radius * 2) + radius; double y = -radius; Color color = RANDOM.nextBoolean() ? Color.RED : Color.GREEN; Circle balloon = new Circle(x, y, radius, color); gamePane.getChildren().add(balloon); } public static void main(String[] args) { launch(args); } } ``` This implementation uses JavaFX to create the game window and handle the animation loop. It creates balloons as Circle shapes with random radius, position, and color. The balloons move down the screen with a constant y velocity that increases with the game speed. The game speed is determined by the time since the game started, and affects the time between balloons appearing and the y velocity of the balloons.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值