Radar Installation
题目:
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.
Figure A Sample Input of Radar Installations
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
方法解析:
大致可以分为两种情况:
1.当所给点的y轴坐标有y>d(雷达影响半径)的情况时,则是无解的情况。
2.当所有点的y轴坐标都不大于d时:
以每一个点为圆心,d为半径画圆。每一个圆都会与x有两个交点,即左点(a表示)和右点(b 表示)(只有一个的也看成有值相等的两个点)。按右点的x轴坐标(即bx)把所有b点所对应的岛屿(输入点)从小到大排序。从左至右,以排好的b点为雷达圆心。每画一个雷达圆,标记ax<=bx的a点所对应的岛屿点,那么下一个圆的圆心坐标为第一个没有标记的岛屿所对应的b点。当有新点(未标记的点)被标记时,雷达个数加一(是处理同一个雷达圆时所遇到的新点,不重复计数)。
............自己都有点绕晕了......= =
代码如下:
1 #include<iostream> 2 #include<cstring> 3 #include<cmath> 4 #include<cstdio> 5 using namespace std; 6 7 class Num 8 { 9 public: 10 double x,y,a,b,p; 11 }; 12 13 #define M 1000 14 15 double ffind(double y,double r) 16 { 17 return sqrt(r*r-y*y); 18 } 19 20 int main() 21 { 22 Num id[M]; 23 Num t; 24 double m,r; 25 int step=0; 26 while(scanf("%lf%lf",&m,&r)!=EOF) 27 { 28 if(m==0 && r==0) break; 29 step++; 30 int i,j,number=0,k=0; 31 for(i=0;i<m;i++) 32 { 33 id[i].x=0; 34 id[i].y=0; 35 id[i].a=0; 36 id[i].b=0; 37 id[i].p=0; 38 } 39 for(i=0;i<m;i++) 40 { 41 scanf("%lf%lf",&id[i].x,&id[i].y); 42 if(id[i].y>r) number=-1; 43 id[i].a=id[i].x-ffind(id[i].y,r); 44 id[i].b=id[i].x+ffind(id[i].y,r); 45 } 46 for(i=0;i<m;i++) 47 for(j=i;j<m;j++) 48 { 49 if(id[j].b<id[i].b) 50 { 51 t=id[j]; 52 id[j]=id[i]; 53 id[i]=t; 54 } 55 } 56 for(i=0;i<m && number>=0;i++) 57 { 58 if(id[i].p==0) 59 { 60 for(j=0;j<m;j++) 61 { 62 if(id[j].p==0 && id[j].a<=id[i].b) 63 { 64 id[j].p=1; 65 k++; 66 } 67 } 68 } 69 if(k>0){ number++;} 70 k=0; 71 } 72 printf("Case %d: %d\n",step,number); 73 } 74 return 0; 75 }