POJ1328-Radar Installation

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

分析:

题意:
X轴是海岸线,Y轴正半轴是海,Y的负半轴是陆地,现在给出N座小岛以及他们的坐标,然后用雷达来保护小岛,雷达布置在海岸线上,雷达的扫描半径为R,问最少要多少颗雷达?

解析:
开始我想着从左边开始布置雷达,在以到第一个小岛的距离恰为R的点布置雷达,以后的雷达以此类推,但是,这个思想是错的,大错特错!
例如下面的案例:
2 3
0 2
1 3
以上面的思想获得的答案是2,但正确答案是1,你细品!

所以上网查看了大佬们的代码,得到了一个新思路!

我们以小岛为圆心作圆,与x轴得两个交点分别是可以放置雷达得极限情况,这样我们就只需要求交集。开始只有一个小岛,则集合即为第一个小岛得左右极限端点,每添加一个小岛就求一次交集,然后更新集合(这些有公共交集得小岛都可以被一个雷达覆盖),如果与新添加得小岛没有交集,那么说明这个新添加的小岛不能与之前有共同交集的小岛们被同一个雷达覆盖,所以我们就要额外的一个雷达!

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define N 1005

using namespace std;

struct node{
	double x,y;
};

node book[N];
node range[N];

bool cmp(node a,node b)
{
	if(a.x==b.x)
		return a.y<b.y;
	return a.x<b.x;
}

int main()
{
	int n,t=0,num;
	double d,Max,Len,len;
	while(~scanf("%d%lf",&n,&d)&&(n+d))
	{
		Max=-1,num=1;
		bool flag=true;
		if(d<0)
			flag=false;
		Len=pow(d,2);
		for(int i=1;i<=n;i++)
		{
			scanf("%lf%lf",&book[i].x,&book[i].y);
			if(book[i].y>Max)
				Max=abs(book[i].y);
			if(book[i].y<0)
				flag=false;
			if(flag)
			{
				len=sqrt(Len-pow(book[i].y,2));
				range[i].x=book[i].x-len;
				range[i].y=book[i].x+len;
			}
		}
		if(Max>d||!flag)
			printf("Case %d: -1\n",++t);
		else
		{
			sort(range+1,range+n+1,cmp);
			double l=range[1].x,r=range[1].y;
			for(int i=2;i<=n;i++)
			{
				if(range[i].x>r)
				{
					num++;
					l=range[i].x;
					r=range[i].y;
				}
				if(range[i].x>l&&range[i].y<r)
				{
					l=range[i].x;
					r=range[i].y;
				}
				if(range[i].x>l&&range[i].y>r)
				{
					l=range[i].x;
				}
			}
			printf("Case %d: %d\n",++t,num);
		}
	}
	return 0;
 } 
发布了46 篇原创文章 · 获赞 16 · 访问量 397

猜你喜欢

转载自blog.csdn.net/weixin_43357583/article/details/105144701