Java实现 蓝桥杯 算法训练 Balloons in a Box

试题 算法训练 Balloons in a Box

问题描述
  你要写一个程序,使得能够模拟在长方体的盒子里放置球形的气球。
  接下来是模拟的方案。假设你已知一个长方体的盒子和一个点集。每一个点代表一个可以放置气球的位置。在一个点上放置一个气球,就是以这个点为球心,然后让这个球膨胀,直到触及盒子的边缘或者一个之前已经被放置好的气球。你不能使用一个在盒子外面或者在一个之前已经放置好的气球里面的点。但是,你可以按你喜欢的任意顺序使用这些点,而且你不需要每个点都用。你的目标是按照某种顺序在盒子里放置气球,使得气球占据的总体积最大。
  你要做的是计算盒子里没被气球占据的体积。
输入格式
  第一行包含一个整数n表示集合里点的个数(1≤n≤6)。第二行包含三个整数表示盒子的一个角落的(x,y,z)坐标,第三行包含与之相对的那个角落的(x,y,z)坐标。接下来n行,每行包含三个整数,表示集合中每个点的(x,y,z)坐标。这个盒子的每维的长度都是非零的,而且它的边与坐标轴平行。
输出格式
  只有一行,为那个盒子没被气球占据的最小体积(四舍五入到整数)。
样例输入
2
0 0 0
10 10 10
3 3 3
7 7 7
样例输出
774
数据规模和约定
  所有坐标的绝对值小于等于1000
  对于20%的数据:n=1
  对于50%的数据:1≤n≤3
  对于100%的数据:1≤n≤6

 

import java.util.Scanner;

public class Main {
	static int n;
	static int[] visit;
	static point d;
	static point u;
	static point[] t;
	static double max;
	static int[] now;
	public static void main(String[] args) {
     Scanner in = new Scanner(System.in);
	 n = in.nextInt();
	 visit=new int[n+1];
	 now=new int[n+1];
	 d=new point(in.nextInt(),in.nextInt(),in.nextInt());
	 u=new point(in.nextInt(),in.nextInt(),in.nextInt());
	 point mins = new point(Math.min(d.x, u.x), Math.min(d.y, u.y), Math.min(d.z, u.z));
	 point maxs = new point(Math.max(d.x, u.x), Math.max(d.y, u.y), Math.max(d.z, u.z));
	 t = new point[n+1];
	 for(int i =1;i<=n;){
		 int x = in.nextInt();
		 int y = in.nextInt();
		 int z = in.nextInt();
		 if(x<mins.x||x>maxs.x||y<mins.y||y>maxs.y||z<mins.z||z>maxs.z){
			 n--;
			 continue;
		 }
		 t[i++] = new point(x,y,z);
	 }
	 in.close();
	 dfs(1);
	 double v = Math.abs((d.x-u.x)*(d.y-u.y)*(d.z-u.z));
	 System.out.println(String.format("%.0f",v-max));
	}
	static void dfs(int i){
		for(int j = 1;j<=n;j++){
			if(visit[j]==0){
				now[i]=j;
				visit[j] = 1;
				if(i==n){
					fun();
				}
				else {
					dfs(i+1);
				}
				visit[j] = 0;
			}
		}
	}
	static void fun() {
		double all=0.0;
		int no;
		double r1;
		double r2;
		double r3;
		double r4;
		for(int i =1;i<=n;i++){
			t[i].r=0.0;
		}
		for(int i=1;i<=n;i++){
			no = now[i];
			r1=Math.min(Math.abs(t[no].x-d.x),Math.abs(t[no].x-u.x));
			r2=Math.min(Math.abs(t[no].y-d.y),Math.abs(t[no].y-u.y));
			r3=Math.min(Math.abs(t[no].z-d.z),Math.abs(t[no].z-u.z));
			t[no].r=Math.min(r1, Math.min(r2, r3));
			for(int j = 1;j<=n;j++){
				if(j==no||t[j].r==0){
					continue;
				}
				r4=Math.sqrt((t[no].x-t[j].x)*(t[no].x-t[j].x)
						+(t[no].y-t[j].y)*(t[no].y-t[j].y)
						+(t[no].z-t[j].z)*(t[no].z-t[j].z))-t[j].r;
				t[no].r=Math.min(t[no].r, r4);
				if(t[no].r<0.0){
					t[no].r=0.0;
				}
				
			}
			all += 4.0/3*Math.PI*Math.pow(t[no].r, 3);
		}
		if(max<all){
			max = all;
		}
	}
}

class point{
	int x;
	int y;
	int z;
	double r;
	public point(int a,int b,int c){
		x=a;
		y=b;
		z=c;
	}
}

原文地址:https://www.cnblogs.com/a1439775520/p/13074899.html