1. 程式人生 > >Radar Installation (pku 1328)雷達放置

Radar Installation (pku 1328)雷達放置

題目:Radar Installation (pku 1328)

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.


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                 Sample Output

3 2                           Case 1: 2

1 2                           Case 2: 1

-3 1

2 1

 

1 2

0 2

 

0 0

 

演算法

1 排序 算出每個島可安雷達的最左邊座標和最右邊座標 左座標按升序排列 左相同時右按照降序排列
2 貪心選取 更新最右邊的座標 如果下一個的左座標大於當前最右邊 數目加一 否則的話更新最右邊的值

#include<stdio.h>
#include<math.h>
#define maxn 1001
struct A
{
    double left,right;
    }a[maxn];
int n,d,m=0;   
void sort(struct A *p)
{
    for(int i=0;i<n;i++)
    {
        int ch=i;
        for(int j=i+1;j<n;j++)
        if(p[j].left<p[ch].left)
        ch=j;
        if(ch!=i)
        {
            double m,n;
            m=p[ch].left;
            p[ch].left=p[i].left;
            p[i].left=m;
            
            
            n=p[ch].right;
            p[ch].right=p[i].right;
            p[i].right=n;
            }
        }
    }
int main()
{
    while(1)
    {
        scanf("%d%d",&n,&d);
        if(n==0&&d==0)
        break;
        m++;
        int judge=0;
        double leida;
        int num=1;
        for(int i=0;i<n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(y>d||d<=0)
            {
                judge=1;
                }
            
            a[i].left=x-sqrt(d*d-y*y);
            a[i].right=x+sqrt(d*d-y*y);
            }
        if(judge)
        {
            printf("Case %d: -1\n",m);
            continue;
                }
        else
        {
            if(n>1)
            {
            sort(a);
            if(a[0].right>=a[1].left)
            {
                if(a[0].right<a[1].right)
                leida=a[0].right;
                else
                leida=a[1].right;
                }
            if(a[0].right<a[1].left)
            {
                leida=a[1].right;
                num++;
                }
            for(int i=2;i<n;i++)
            {
                if(leida>=a[i].left)
                {
                    if(leida>a[i].right)
                    leida=a[i].right;
                    }else
                {
                    leida=a[i].right;
                    num++;
                    }
                }
            }
        }
            printf("Case %d: %d\n",m,num);
        }
    
    
    return 0;
    }