Problem L

Problem 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.
Problem <wbr>L
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.< br>< br>The input is terminated by a line containing pair of zeros< br>

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
题意:一条无限长的X轴表示海岸线,上边是海,下面是陆地,海中有很多岛屿,现在要在海岸线上安装雷达半径为r,求最少安装多少雷达就可以把所有的岛屿覆盖;
解题思路:第一遍心思着用把每个岛屿以r为半径的圆与x轴相交的地方表示出来,看多少重叠部分就多少雷达,但是相交的部分是浮点的,所以没法表示,所以可以先按照左交点排序,看以后的左交点是不是能和前一个的右交点相交,能相交两个圆就有重合部分,就能共用一个雷达,另外要判断这些圆是不是与x轴有交点,没有的或就输出-1;
感悟:刚上完体育课,真有感觉啊!
代码(G++)
#include
#include
#include
#include
#define maxn 1010
using namespace std;
struct land
{
    double x;
    double y;
    double lx;
    double ly;
};
bool comp(const land &a,const land &b)
{
    return a.lx
}
double landlx(double x,double y,int r)
{
    return x-sqrt(r*r-y*y);
}
double landly(double x,double y,int r)
{
    return x+sqrt(r*r-y*y);
}
int main()
{
    //freopen("in.txt", "r", stdin);
    int n,ans,c=1,r;
    land l[maxn];
    while(~scanf("%d%d",&n,&r)&&n&&r)
    {
        ans=1;
        for(int i=0;i
        {
            scanf("%lf%lf",&l[i].x,&l[i].y);
            l[i].lx=landlx(l[i].x,l[i].y,r);
            l[i].ly=landly(l[i].x,l[i].y,r);
            if(l[i].y>r||r<=0||l[i].y<0)//这样不可能组成圆
                ans=-1;
        }//求出来岛屿在海岸线上的交点
        scanf("\n");
        sort(&l[0],&l[n],comp);
        double  round_y=l[0].ly;//令一个量等于排完序第一个的与X轴的右交点
        for(int i=1;i
        {
            if(l[i].lx>round_y)//两个圆不相交没法共用一个雷达
            {
                ans++;
                round_y=l[i].ly;
            }
            else if(l[i].ly
            {
                round_y=l[i].ly;
            }
        }
        printf("Case %d: %d\n",c++,ans);
    }
}
posted @ 2016-03-21 16:43  勿忘初心0924  阅读(161)  评论(0编辑  收藏  举报