Radar Installation POJ

来源:互联网 发布:淘宝捉猫猫是干嘛的 编辑:程序博客网 时间:2024/06/03 21:51
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 21 2-3 12 11 20 20 0
Sample Output
Case 1: 2Case 2: 1


  看到这个题,首先可能会想到根据岛屿的位置的选取达到范围内最靠右的雷达,让雷达在去覆盖点,雷达覆盖不到的点,更新结点,在选取靠右的点
直达结束。。。。很显然这个方法是错误的。。有可能存在一种情况你建立的雷达点覆盖不到的一个节点,稍微往左移一点,会覆盖结点。。

有时候对题不能太死板,应该换一种思路。。不按雷达为出发点思考。。以岛屿为点画圆,与坐标轴存在相交的两个点,在此范围内寻满足条件的点。与其他点可能会存在相交的部分,在此部分建立雷达。。

#include<cstdio>#include<cmath>#include<algorithm>using namespace std;const int M=1010;struct Point{    double x1;    double x2;}point[M];bool cmp(Point a,Point b){    return a.x1<b.x1;}int main(){    int n,m;    int x,y,f,p=0,ans;    while(scanf("%d%d",&n,&m)!=EOF)    {        if(n==0&&m==0)            break;        f=1;ans=0;        for(int i=0;i<n;i++)        {            scanf("%d%d",&x,&y);            if(f==0)            continue;            if(y>m)             {                f=0;            }            else            {            point[i].x1=(double)x-sqrt((double)m*m-(double)y*y);            point[i].x2=(double)x+sqrt((double)m*m-(double)y*y);            }        }        if(f==0)            printf("Case %d: -1\n",++p);        else        {            ans++;            sort(point,point+n,cmp);            double now=point[0].x2;            for(int i=1;i<n;i++)            {                if(now>point[i].x2)                now=point[i].x2;                else if(now<point[i].x1)                {                    ans++;                    now=point[i].x2;                }            }            printf("Case %d: %d\n",++p,ans);        }    }    return 0;}


原创粉丝点击