POJ 1328 Radar Installation (贪心)

Radar Installation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 44937 Accepted: 9970

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

3 2
1 2
-3 1
2 1

1 2
0 2

0 0

Sample Output

Case 1: 2
Case 2: 1

Source

Beijing 2002


题意:
有一个平面坐标,当前假设 x 轴上方为大海,在海上有 n 座岛屿,需要在海岸线上( x 轴)建造一个个雷达去覆盖海上的岛屿,而且雷达都是统一的,具有相同的覆盖面积,其半径为 d,要求输出能覆盖所有岛屿的最小雷达数,未满足要求时输出 -1 。
思路:
想法很简单,就是贪心的思路,一个圆尽量覆盖更多的点(岛屿)。
只需要将点的坐标按 x 进行排序,先处理左边的点,依次往右边处理。
有两个地方需要注意一下:
1、如下图:

我们就此图来讨论以下,若只有这三个点,那么排序1,2,3是没有错的,当扫描到第一个点的时候,可以将 1 点坐标和半径 d 以及圆心在 x 轴上三个条件来确定一个圆,圆心为c1,正好圆c1没有覆盖到点 2 (也可说是左边蓝色区域再去掉1点 x 值左边的区域)。
此类特殊点的处理方法是将点 2 所能定位的圆心 c2 重新作为该个雷达能扫描的圆心位置(雷达摆放位置),为什么这么简单的一个调整就能解决问题呢?
简单证明:
“左边蓝色区域再去掉1点 x 值左边的区域”内的点到 c1 点的距离一定大于半径 d ,那么以该区域内的点作为圆心画圆,与 x 轴相交的点(例 c2 圆心)一定在 c1 的左边,那么与 x 轴相交的点(例 c2)到 1 点距离一定小于半径 d ,即一定能够覆盖 1 点,对吧。
这样话,就是将圆心稍微忘左移一些的意思,使得一个圆尽量覆盖更多的圆,和起始的思路达成一致。
还有一个点 3 就一定是需要一个新雷达的
2、一个本人犯傻的地方,就是输出“-1”的地方忘了输出“Case num:”,一直错在这个地方,后来试着举了个不符合条件的例子,才发现这个漏洞~

/*************************************************************************
	> File Name: POJ1328.cpp
	> Author: BSlin
	> Mail:  
	> Created Time: 2013年11月03日 星期日 19时26分56秒
 ************************************************************************/

#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
using namespace std;

struct node {
    double x,y;
    bool operator < (const node b) const {
        return x < b.x;
    }
}a[1010];

double d;

double find_point(node a) {
    return a.x + sqrt(d * d - a.y * a.y);
}

double get_dis(double x, node a) {
    return sqrt((a.x - x) * (a.x - x) + a.y * a.y);
}

int main() {
    freopen("in.txt","r",stdin);
    int n, cnt, k = 1;
    double nowx,nowd;
    bool flag;
    while(scanf("%d%lf",&n,&d)) {
        if(n == 0 && d == 0)
            break;
        flag = false;
        for(int i=0; i<n; i++) {
            scanf("%lf%lf",&a[i].x,&a[i].y);
            if(a[i].y > d || a[i].y < 0) {
                flag = true;
            }
        }
        if(flag == true) {
            printf("Case %d: -1\n",k++);
            continue;
        }
        sort(a,a+n);
        cnt = 0;
        for(int i=0; i<n; i++) {
            cnt ++;
            nowx = find_point(a[i]);
            for(i=i+1; i<n; i++) {
                nowd = get_dis(nowx,a[i]);
                if(nowd > d && a[i].x < nowx) {
                    nowx = find_point(a[i]);
                }
                if(nowd > d && a[i].x > nowx) {
                    break;
                }
            }
            i--;
        }
        printf("Case %d: %d\n", k++, cnt);
    }
}






  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值