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
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
翻译:
假设滑行是一条无限长的直线。陆地在海岸的一边,海洋在另一边。每个小岛都是位于海边的一个点。而任何一个位于海岸线上的雷达装置,都只能覆盖d距离,所以一个半径装置可以覆盖海面上的一个岛屿,如果它们之间的距离最多是d。我们使用笛卡尔坐标系,定义海岸是x轴。海侧在x轴上方,陆侧在x轴下方。考虑到每个岛屿在海洋中的位置,考虑到雷达装置的覆盖距离,你的任务是编写一个程序,找到覆盖所有岛屿的雷达装置的最小数量。请注意,岛的位置由其x-y坐标表示。
思路:
要求雷达数,我们不妨把每个小岛看成一个圆,如果这个圆有交集则说明这个两个岛可以共用一个雷达,反之则不能共用一个雷达。
这个题其实就像是找到元素个数最小的集合,使得每个区间至少有一个属于该集合;
代码:
#include<iostream>
#include<algorithm>
#include<math.h>
#include<stdio.h>
#include<cstring>
#include <queue>
using namespace std;
struct chh{
double c,d;
}arr[1009];
bool cmp(const chh &a,const chh &b)
{
return a.d<b.d;
}
int main()
{
int n;
double d;
int a=0;
while(cin>>n>>d)
{
int b=1;
if(n==0&&d==0)
break;
a++;
int c=0;
for(int i=1;i<=n;i++)
{
int x,y;
cin>>x>>y;
if(y<-d||y>d)
c=1;
double t=sqrt(d*d-y*y);
arr[i].c=x-t;
arr[i].d=x+t;
}
if(c==1)
{
cout<<"Case "<<a<<": "<<-1<<endl;
continue;
}
sort(arr+1,arr+n+1,cmp);
double d=arr[1].d;
for(int i=2;i<=n;i++)
{
if(arr[i].c>d)
{b++;d=arr[i].d;}
}
cout<<"Case "<<a<<": "<<b<<endl;
}
}