Radar Installation

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.
Radar Installation
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

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

海岛和雷达问题,看看怎样用最少的雷达覆盖所有海岛;
输入 3 2 是海岛数 和雷达覆盖半径;
接下来是 : 海岛坐标
我感觉这题好奇葩,测试数据都过了,就是提交不上,说明你还是有地方用了整形(%d),看看能换的全部用double 试试;
下面是我同学的代码……我的是数据都过 ……就是WA
 1 #include <stdio.h>

 2 #include<string.h>

 3 #include<stdlib.h>

 4 #include<math.h>

 5 using namespace std;

 6 struct node

 7 {

 8    double a,b;

 9 }area[1005];

10 int cmp(const void *a,const void *b)

11 {

12     return (*(node *)a).b>(*(node *)b).b?1:-1;

13 }

14 int main()

15 {

16     int n,i,sum,f,h=1;

17     double x,y,d,e;

18     while(~scanf("%d%lf",&n,&d)&&(n!=0||d!=0))

19     {

20          f=0;   sum=1;

21         for(i=0; i<n; i++)

22         {

23             scanf("%lf%lf",&x,&y);

24             area[i].a=x-sqrt(d*d-y*y);//这里是坐标转区间

25             area[i].b=x+sqrt(d*d-y*y);

26             if(y<0)

27             y=-y;

28             if(y>d)

29             f=1;

30         }

31 

32         if(f)

33         printf("Case %d: -1\n",h);

34 

35         else

36         {

37             qsort(area,n,sizeof(area[0]),cmp);

38             e=area[0].b;

39             for(i=1; i<n; i++)

40             {

41                 if(area[i].a>e)//找左边界小于该右边界的

42                 {

43                     sum++;

44                     e=area[i].b;

45                 }

46             }

47             printf("Case %d: %d\n",h,sum);

48         }

49         h++;

50     }

51     return 0;

52 }
View Code

 

你可能感兴趣的:(Install)