这是一道有趣的贪心问题,首先判断-1的情况,当岛屿当x轴的距离大于了雷达侦测的半径,就输出-1。然后,需要找到每一岛屿对应能侦测到这个岛屿雷达的放置范围。假设每一个岛屿需要一个雷达,再减去雷达重合的数量。
#include<cstdio>
#include<algorithm>
#include<math.h>
#include<cstring>
using namespace std;
struct land{ //x,y表示岛屿的位置,x1,x2表示雷达放置的范围
int x,y;
double x1,x2;
};
int cmp(struct land a, struct land b)
{
a.x2 < b.x2;
}
int main()
{
int n,d;
int t1=0;
while(1)
{
int count=0;
int num[1002]; //当有雷达侦测时为1,没有为0
memset(num,0,sizeof(num));
struct land t[1002];
scanf("%d%d",&n,&d);
if(n==0 && d==0)
break;
for(int i=0;i<n;i++){
scanf("%d%d",&t[i].x,&t[i].y);
}
int flag=0;
for(int i=0;i<n;i++)
if(t[i].y>d){
printf("Case %d: -1\n",++t1);
flag=1;
break;
}
if(flag==1)
continue;
for(int i=0;i<n;i++) //算x1,x2
{
double o=sqrt(d*d*1.0-t[i].y*t[i].y*1.0);
t[i].x1=t[i].x-o;
t[i].x2=t[i].x+o;
}
sort(t, t + n, cmp);
for(int i=0;i<n;i++){
if(num[i]==0)
{
count++;
num[i]=1;
for(int j=0;j<n;j++)
{
if( num[j]==0 && t[j].x1<=t[i].x2) //去掉重复的岛屿
num[j]=1;
}
}
}
printf("Case %d: %d\n",++t1,count);
}
return 0;
}