Blue Bridge 2019 Question One

Blue Bridge 种树问题

Problem Description:

Xiaoming and his friends went to plant trees in the suburbs. They brought some small seedlings that were carefully researched in their own laboratory.
There are n people in Xiao Ming and his friends. After careful selection, each person selected a suitable location for planting trees in a clearing, a total of n. They are going to plant all the saplings they brought.
However, they encountered a difficulty: some saplings were relatively large, and some were too close together, causing the two trees to collide when planted.
They saw the tree as a circle, with the center of the circle at the location they were looking for. If the circles corresponding to the two trees intersect, the two trees are not suitable for planting at the same time (tangent is not affected), which is called a conflict between the two trees.
Xiaoming and his friends decided to sum up first and only plant a part of them to ensure that there are no conflicting trees. They also hope that these trees can cover the largest area (circle area).

Input format:

The first line of the input contains an integer n, which represents the number of people, that is, the number of positions to be planted.
The next n rows, each row of three integers x, y, r, represents the horizontal, vertical, and radius of a tree on the open ground.

Output format:

The output line contains an integer that represents the sum of the area that can be planted without conflict. Since the area of ​​each tree is an integer multiple of pi, please output the value divided by the pi (it should be an integer).

Sample input:

6
1 1 2
1 4 2
1 7 2
4 1 2
4 4 2
4 7 2

Sample output:

12

PS:

  1. The problems of my Blue Bridge Cup are mostly math problems. I ca n’t think of a good algorithm and a good solution. I may not be able to write a program (or a good program). The knowledge points surveyed are actually simple knowledge points that coexist in many languages, which is different from the actual problems encountered in actual development.
  2. This question hasn't seen many netizens write the Java version of the answer program on the Internet, share here. FH don't say much, put on the code!
import java.util.Scanner;
import java.lang.Math;

public class HelloWorld{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int numOfPerson = sc.nextInt();
		
		// 将树的圆心坐标 x,y及其半径r保存在一个二维数组中
		int[][] tree = new int [numOfPerson][3];
	
		// 存放种下的树的信息
		int[][] plant = new int [numOfPerson][3];
		
		int area = 0;// 因为不用加pi
		
		for(int i = 0;i < numOfPerson;i++) {
			
			tree[i][0] = sc.nextInt(); 
			tree[i][1] = sc.nextInt();
			tree[i][2] = sc.nextInt();
		}	
		sc.close();
		
		// 第一棵树直接种下
		plant[0][0] = tree[0][0];
		plant[0][1] = tree[0][1];
		plant[0][2] = tree[0][2];
		
		// 后面的每棵树与前面种下的每棵树进行比较
		for(int i = 1;i<tree.length;i++) {
			// 防止进行多余的计算,如果某棵树没有种下,则新的树不与该位置的000进行比较
			int j = 0;
			
			for(;j<plant.length;j++) {
				if(plant[j][0] == 0 && plant[j][1] ==0 && plant[j][2] == 0) {
					continue;
				}
				// 如果中间过程中就不满足条件,直接结束内层循环,进入下一次的外层循环
				if(Math.sqrt(Math.pow(tree[i][0]-plant[j][0], 2) + Math.pow(tree[i][1]-plant[j][1], 2)) 
						< (tree[i][2] + plant[j][2])){
					break;
				}
			}
			//如果此时是全部都判断了一次,退出循环不是因为不满足题设条件,而是因为循环判断的条件,则种树
			if(j == plant.length) {
				plant[i][0] = tree[i][0];
				plant[i][1] = tree[i][1];
				plant[i][2] = tree[i][2];
			}
			
		}
		// 计算所有种下的树的占地面积	
		for(int i = 0;i < plant.length;i++) {
			area += Math.pow(plant[i][2], 2); 
		}
		System.out.println(area);
		
	}	
}

运行结果:

6
1 1 2
1 4 2
1 7 2
4 1 2
4 4 2
4 7 2
12
  1. In the process of writing this question, I used Eclipse. I used to use IDEA before, but I am not used to it;
  2. I also encountered a very embarrassing problem during the writing process. 平方I wrote the operation ^2. Affected by other languages, I never found this low-level error, which led to a long time to find the error, and then I realized it after seeing the API documentation. Basic knowledge is not solid enough! It's so far away!
  3. Sometimes I think that the question should not be very complicated. When I first wrote it, I thought it was too complicated. Later, I thought of the simple and effective method above (but whether it can meet the requirements of the official test is not easy to say ...).

Guess you like

Origin www.cnblogs.com/yerun/p/12727394.html