Radar Installation(雷达安装)c语言

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

输入
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
输出
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.
题目描述
假设滑行是一条无限的直线。陆地在滑行的一侧,大海在另一侧。每个小岛都是位于海边的一个点。而任何安装在滑行上的雷达,都只能覆盖d距离,所以海中的一个岛屿可以被一个半径装置覆盖,如果它们之间的距离最多是d.

我们使用笛卡尔坐标系,定义滑行是x轴。海侧在 x 轴上方,陆地侧在下方。给定每个岛屿在海中的位置,并考虑到雷达装置的覆盖范围的距离,您的任务是编写一个程序来找到覆盖所有岛屿的最小数量的雷达装置。请注意,岛屿的位置由其 x-y 坐标表示。

图 雷达装置输入示例

输入
输入由多个测试用例组成。每个情况的第一行包含两个整数 n (1<=n<=1000) 和 d,其中 n 是海洋中的岛屿数,d 是雷达装置的覆盖距离。后跟 n 行,每行包含两个整数,表示每个岛屿位置的坐标。然后,下面用一个空行分隔案例。

输入由包含一对零的线终止
输出
对于每个测试用例输出,一行由测试用例编号组成,后跟所需的最小数量的雷达安装。“-1”安装意味着没有针对该情况的解决方案。

#include<stdio.h>
#include<math.h>
#define max 1010
struct leida
{
    double left,right;
    }a[max];
int n,d,m=0;   
void sort(struct leida *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 t=0;
        double time;
        int num=1;
        for(int i=0;i<n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(y>d||d<=0)
            {
                t=1;
                }
            
            a[i].left=x-sqrt(d*d-y*y);
            a[i].right=x+sqrt(d*d-y*y);
            }
        if(t)
        {
            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)
                time=a[0].right;
                else
                time=a[1].right;
                }
            if(a[0].right<a[1].left)
            {
                time=a[1].right;
                num++;
                }
            for(int i=2;i<n;i++)
            {
                if(time>=a[i].left)
                {
                    if(time>a[i].right)
                    time=a[i].right;
                    }else
                {
                    time=a[i].right;
                    num++;
                    }
                }
            }
        }
            printf("Case %d: %d\n",m,num);
        }
    
    
    return 0;
    }
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值