北大acm1328,贪心算法

北大acm1328,贪心算法

(大大白第一次写博客,望谅解)
题目:
**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**

**题目大意:**本题是贪心算法的一题解题思路,本题的题目是要我们建立雷达,给出我们雷达可以达到的半径和小岛数量以及小岛的位置,需要我们计算最小雷达数,这个数量是可以将给出的小岛全都侦测到。

**解题思路:**我们以每个小岛的位置为圆心,雷达侦测半径为圆半径做圆,每个小岛均有何x轴的两个交点,我们将左交点排序,按照贪心算法的思想确认``每个雷达的位置,从而计算最少需要多少个雷达。

实现源码如下:

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

struct Dir//struct和typedef struct
{
	double left;//与x轴的左交点
	double right;//与x轴的右交点
}dir[2000];//坐标轴

bool cmp(Dir x,Dir y)//sort内部函数需要用到
{
	if(x.left <= y.left)
		return true;
	else
		return false;
}

int main()
{
	int n,rid,num=1;;//https://www.cnblogs.com/TX980502/p/8528840.html//岛屿数量,雷达半径
	while(cin>>n>>rid && n && rid )
	{
		int flag=1;
		/*if(n==0 && rid==0)
		{
			return 0;
		}*/	
		for(int i=0;i<n;i++)
		{
			double x,y,z;
			cin>>x>>y;//岛屿的横纵坐标
			if(y > rid)
				flag=0;
			z=sqrt(double(rid*rid)-double(y*y));
			dir[i].left=x-z;//计算岛屿和x轴的左交点
			dir[i].right=x+z;//计算岛屿和x轴的右交点
		}
		if(flag)
		{
			sort(dir,dir+n,cmp);//如何以left为对比元素 让整个结构体都有序,排序函数
			int island=1;//默认值为1
			double min_right=dir[0].right;//定义初始右交点为第一个值的右交点
			for(int j=1;j<n;j++)
			{
				if(dir[j].left > min_right)//左交点大于第一个的右交点 把第一个灯塔立在最左边岛和x轴的右交点上
				{
					island++;
					min_right=dir[j].right;
				}
				else if(dir[j].right < min_right)//右交点也小于 证明区间包含关系 将灯塔建在内部岛和x轴的右交点上
				{		
					min_right=dir[j].right;
				}
			}
			cout<<"Case "<<num<<": "<<island<<endl; 
			num++;
		}
		else
		{
			int island=-1;//异常输出为-1
			cout<<"Case "<<num<<": "<<island<<endl; 
			num++;
		}
	}
	return 1;
}

猜你喜欢

转载自blog.csdn.net/weixin_43626529/article/details/86667159