先计算出以每个小岛为圆心,算出每个小岛与海岸的左右交点。
之后进行比较,有以下三种情况:
WA了很多次,最后终于AC的代码:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct Land
{
int x;
int y;
double x_r;
double x_l;
} land[1100];
void sort(struct Land land[],int n){
int i,j;
struct Land temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-1-i;j++)
{
if(land[j].x_l>land[j+1].x_l)
{
temp=land[j];
land[j]=land[j+1];
land[j+1]=temp;
}
}
}
}
int main()
{
int i,j,n,r,k=1,outcome=1;
while(1)
{
scanf("%d %d",&n,&r);
if(n==0&&r==0)
break;
for(i=0;i<n;i++)
{
scanf("%d %d",&land[i].x,&land[i].y);
if(land[i].y<0||land[i].y>r)
{
outcome=-1;
//不能加break 加了就会WA
}
land[i].x_l=land[i].x-sqrt((double)r*r-land[i].y*land[i].y);
land[i].x_r=land[i].x+sqrt((double)r*r-land[i].y*land[i].y);
}
if(outcome==-1)
{
printf("Case %d: -1\n",k);
k++;
outcome=1;//在输出结束之后也不能忘记改变k和outcome的值
continue;
}
sort(land,n);
for(i=1,j=0;i<n;i++)
{
if(land[j].x_r>land[i].x_r)
j=i;
if(land[j].x_r<land[i].x_l)
{
outcome++;
j=i;
}
}
printf("Case %d: %d\n",k,outcome);
k++;
outcome=1;
}
return 0;
}