算法训练 小生物的逃逸

问题描述
  空间中有n个球,这些球不相交也不相切。有m个可以视为质点的小生物,可能在某些球内,也可能在所有球之外,但不会在球面上。问这些生物从原来的地方逃逸到所有球外面的空间,至少要经过多少层球面。
输入格式
  第一行两个数n、m:表示球的数量和小生物的数量;
  接下来n行每行四个整数Xi、Yi、Zi和Ri:表示一个球的三维坐标和半径;
  接下来m行每行三个整数Xi、Yi、Zi:表示一个生物的坐标。
输出格式
  一行m个数:表示每个小生物逃逸时至少经过的球面数。
样例输入
2 2
0 0 0 2
0 0 0 4
0 0 1
0 0 3
样例输出
2 1
数据规模和约定
  1<=n、m<=100,|Xi|、|Yi|、|Zi|<=10000,1<=Ri<=10000;
  数据保证所有球严格不接触,小生物都不在球面上。
 
 
主要求圆心与圆心之间的距离和圆的半径比较大小
/**
 * 
 */
package com.xingbing.lanqiao;

import java.util.Scanner;

/**
 * @author 
 * @data
 * @description
 */
public class 小动物的逃逸 {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();//表示球的数量
		int m = in.nextInt();//表示小动物的数量
		int a[][] = new int[n][4];//用来保存球的坐标和半径
		int b[][] = new int[m][3];//用来保存小动物的坐标
		for(int i=0;i<n;i++){
			for(int j=0;j<4;j++){
				a[i][j] = in.nextInt();
			}
		}
		for(int i=0;i<m;i++){
			for(int j=0;j<3;j++){
				b[i][j] = in.nextInt();
			}
		}
		int count = 0;
		for(int i=0;i<m;i++){
			count=0;
			for(int j=0;j<n;j++){
				double r = (Math.pow(a[j][0]-b[i][0], 2)+Math.pow(a[j][1]-b[i][1], 2)+Math.pow(a[j][2]-b[i][2], 2));
				if(r<Math.pow(a[j][3], 2)){
					count++;
				}
			}
			System.out.print(count+" ");
		}
	}
}

  

猜你喜欢

转载自www.cnblogs.com/xuesujun/p/12685861.html