poj1328雷达安装【贪心法】

问题简述:x轴上方有很多岛屿,在x轴上放雷达,用最少的雷达使得所有岛能够被覆盖到

原问题:

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

思路:

对一个岛屿,如果要放一个雷达来覆盖它,我们可以算出一个区间,表示这个雷达最左和最右分别能放到什么地方的时候仍然能保证覆盖到它(以该点为圆心,以覆盖半径d为半径画一个圆,该圆与x轴相交的那一段就是我们要求的区间),暂且把这个区间叫做覆盖区间。

从最左边的点(岛屿)开始看,观察它的覆盖区间,如果要放一个雷达,直观上我们更希望放到区间的最右端,这样能覆盖更多右边剩下的点;

看下一个点,如果它的覆盖区间与上一个点的区间有重叠,就说明如果把雷达放在那个重叠的部分时就可以同时覆盖这两个点。

再看第三个点,如果第三个点的覆盖区间也与前两个点有重叠,那么要看是不是三个点都重叠在一起;如果不是,那这第三个点其实并不能与前两个点公共用一个雷达;如果三个区间有一块确实重叠在一起了,那说明三个点可以共用同一个雷达。

因此,我们判断是不是要再加一个雷达的关键,就是那个“第一个点”的区间与后续几个点的区间有无重叠,第一个点的最右如果大于其余点的最左,就不用加雷达。反之,如果出现了一个点,它的最左大于“第一个点”的最右了,就说明不能共用了,要再加一个雷达,而后面的点的最左就要跟这个新的“第一个点”来比较了。

所以我们可以设一个变量来记录当前的“第一个点”的最右侧,遍历点集更新这个值,这个值每变一次就说明要加一个雷达。

 

代码:

#include <iostream>

#include <cmath>

#include <algorithm>

using namespace std;

 

int n,d;//n是点数,d是雷达覆盖范围半径

 

struct point

{

    double left;

    double right;

}range[20001];//存放每个点的覆盖范围

 

int cmpr(point a,point b)//排序规则(按每个点的覆盖范围最右端来排序)

{

    return a.right < b.right;

}

 

int main()

{

    int k = 1;

    while (cin >> n >> d && n != 0)

    {

        int ans = 1;//需要几个雷达

        bool able = 1;//无解判断,如果无解则able = 0,不再参与后续运算

        if ( d < 0 )

            able = 0;

        for (int i = 0; i < n; i ++)

        {

            double x,y;

            cin >> x >> y;

            if (y > d || y < 0)

                able = 0;

            range[i].left = x - sqrt(d*d - pow(y,2));//可以装雷达的范围(左侧)

            range[i].right = x + sqrt(d*d - pow(y,2));//可以装雷达的范围(右侧)

        }

        if (able != 0)

        {

            sort(range,range+n,cmpr);

            

            double rangeMAXr = range[0].right;//当前能覆盖到的最大范围(右侧)

            for (int i = 0; i < n; i++)//扫一遍

            {

                if (range[i].left > rangeMAXr)//如果这个点超过了当前能覆盖的最大范围(不能被覆盖到),就要加一个雷达

                {

                    rangeMAXr = range[i].right;

                    ans++;

                }

            }

        }

        if (able == 0)

            cout << "Case " << k << ": " << -1 << endl;

        else

            cout << "Case " << k << ": " << ans << endl;

        k++;

    }

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据您的问题,我理解您想了解POJ1328问题的Java解决方案。POJ1328是一个经典的问题,也被称为"雷达安装"问题,它涉及到在一个二维平面上安装雷达以覆盖所有岛屿的最小雷达数量。以下是一个Java解决方案的示例代码: ```java import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int caseNum = 1; while (true) { int n = scanner.nextInt(); if (n == 0) { break; } Island[] islands = new Island[n]; for (int i = 0; i < n; i++) { int x = scanner.nextInt(); int y = scanner.nextInt(); islands[i] = new Island(x, y); } int radarNum = getRadarNum(islands); System.out.println("Case " + caseNum + ": " + radarNum); caseNum++; } } private static int getRadarNum(Island[] islands) { Arrays.sort(islands, Comparator.comparingInt(Island::getRight)); int radarNum = 1; int rightMost = islands[0].getRight(); for (int i = 1; i < islands.length; i++) { if (islands[i].getLeft() > rightMost) { radarNum++; rightMost = islands[i].getRight(); } else { rightMost = Math.min(rightMost, islands[i].getRight()); } } return radarNum; } static class Island { private int left; private int right; public Island(int left, int right) { this.left = left; this.right = right; } public int getLeft() { return left; } public int getRight() { return right; } } } ``` 这段代码通过输入岛屿的坐标,计算出需要安装的最小雷达数量,并输出结果。它使用了排序和贪心算法的思想来解决问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值