Radar Installation
Problem Description
Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d.
We use Cartesian coordinate system, defining the coasting is the x-axis. The sea side is above x-axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x-y coordinates. Figure A Sample Input of Radar Installations
We use Cartesian coordinate system, defining the coasting is the x-axis. The sea side is above x-axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x-y coordinates.
Input
The input consists of several test cases. The first line of each case contains two integers n (1<=n<=1000) and d, where n is the number of islands in the sea and d is the distance of coverage of the radar installation. This is followed by n lines each containing two integers representing the coordinate of the position of each island. Then a blank line follows to separate the cases.
The input is terminated by a line containing pair of zeros
The input is terminated by a line containing pair of zeros
Output
For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. "-1" installation means no solution for that case.
Sample Input
3 2
1 2
-3 1
2 1
1 2
0 2
0 0
Sample Output
Case 1: 2
Case 2: 1
Source
PKU
解题思想:输入各个点;计算出每一个点的圆心在x轴上的范围,然后以圆心范围的前头为标准进行升序排序,让后利用贪心算法进行统计所需雷达的个数;
解题代码:
1 #include<iostream>
2 #include<cmath>
3 #include<cstdio>
4 #include <algorithm>
5 using namespace std;
6 const int Max(1005);
7 struct data
8 {
9 double x1;
10 double x2;
11 }point[Max]; //定义圆心范围的结构体,并且同时定义一个圆心范围的结构体数组;
12 int cmp(data a, data b) //定义sort(快排函数)的比较函数;
13 {
14 return (a.x1-b.x1) < 10e-7;
15 }
16
17 int main()
18 {
19 int step=1;
20 while(1)
21 {
22 int flag=0; //标志位,表示是否有一个点无论如何都不能被检测到;
23 int n,d;
24 cin>>n>>d;
25 if(n==0&&d==0)
26 break;
27 int a,b;
28 for(int i=0;i<n;i++) // 注意这边i不能从1开始,因为后面用到了sort排序
29 { //输入各个点,并计算圆心范围;
30 cin>>a>>b;
31 if(!flag&&(b<= d))
32 {
33 double temp = sqrt(double(d*d-b*b)); //由点处理出圆心的范围
34 point[i].x1 = a-temp;
35 point[i].x2 = a+temp;
36 }
37 else
38 flag=1;
39
40 }
41 if(flag==1)//存在一个点不可能被检测到;
42 {
43 cout<<"Case "<<step<<": "<<"-1"<<endl;
44 step++;
45 }
46 else
47 {
48 int sum=1;
49 sort(point, point+n, cmp);
50 double temp=point[0].x2;
51 for(int i=1;i<n;i++)
52 {
53 if(point[i].x1-temp>10e-7)
54 {
55 sum++;
56 temp=point[i].x2;
57 }
58 else
59 {
60 if(point[i].x2-temp< 10e-7) //当走到的点的右范围比原来的右范围小,即temp相应的改
61 temp = point[i].x2;
62 }
63 }
64 cout<<"Case "<<step<<": "<<sum<<endl;
65 step++;
66 }
67 }
68 return 0;
69
70 }