POJ:1328 Radar Installation 思路以及测试数据

POJ 1328 java AC POJ 1328 java做!雷达问题!java版本!AC答案~ 立即下载
Radar Installation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 76845 Accepted: 17213

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


这道题乍一看还是很好做的,结果做了一天。主要是思路不对,考虑不周到。
下面是我的 错误思路以及反例的测试样例:
思路一:
1)将小岛从左到右编号1~n。
2)访问第一个小岛,根据小岛坐标计算出雷达在海岸线上的坐标范围,取该区间的最右侧建立雷达。
3) 继续访问下一座小岛,如果该小岛处于已经建立的雷达范围内,跳过。如果不在范围内,同2)建立新的雷达站。
4)访问所有的小岛,结束。
可以用以下测试样例判断:
2 2
1 1
2 2

0 0
Case 1:1

思路二:
1)根据每座小岛的坐标计算出每座小岛对应海岸线上可以放置雷达的范围。
2)按输入的顺序对小岛编号,从编号1开始求出与小岛 i 具有共同雷达范围的其他小岛 i1,i2,i3......,将 i,i1,i2,i3......组成集合,并置visit[i], visit[i1],visit[i2],visit[i3]为1,代表已经访问过。
3)遍历所有的小岛,得到N个集合,N即为雷达站的个数。
可以用以下测试样例判断:
9 3
0 2
-3 2
-4 2
-5 2
3 2
6 2
9 2
12 2
15 2
0 0
Case 1:4

正确的思路:与思路二很相近,只不过增加了一步排序。
1)根据每座小岛的坐标计算出每座小岛对应海岸线上可以放置雷达的范围。
2) 将每个小岛对应的雷达设置范围进行排序,使得每个雷达范围的最小值递增。
3)按照2)的顺序对小岛编号,从编号1开始求出与小岛 i 具有共同雷达范围的其他小岛 i1,i2,i3......,将 i,i1,i2,i3......组成集合,并置visit[i],visit[i1],visit[i2],visit[i3]为1,代表已经访问过。
4)遍历所有的小岛,得到N个集合,N即为雷达站的个数。
测试样例:
3 2
1 0
-3 -1
2 1

0 0
Case 1: -1

2 2
1 1
2 2

0 0
Case 2: 1

5 4
-2 2
1 2
0 3
3 3
4 3
0 0
Case 3: 1

下面贴出AC源码:

#include<iostream>
#include<list>
#include<vector>
#include<cmath>
#include<algorithm>
#include<set>
#include<fstream>
using namespace std;

struct node
{
	float l;
	float r;	
};

bool comp(const node &a,const node &b)
{
	return a.l<b.l;	
}

int subsetn(vector<node> s);
bool isIntersect(node a,node b);
node calIntersect(node a,node b);

int main()
{
	int n;	
	float d;
	cin>>n>>d;
	for(int i=0;n!=0||d!=0;cin>>n>>d,i++)
	{
		//save the data!
		vector<pair<float,float> >v(n);
		for(int j=0;j<n;j++)
		{
			cin>>v[j].first>>v[j].second;
		}
		//decide the condition.
		bool flag = true;
		if(d<0)
		{
			cout<<"Case "<<i+1<<": "<<-1<<endl;	
			flag = false;
		}
		else
		{
			for(int j=0;j<n;j++)
			{
				if(v[j].second>d || v[j].second<0)
				{
					cout<<"Case "<<i+1<<": "<<-1<<endl;
					flag = false;
					break;
				}	
		    }			 
		}
		//solve the problem when flag == true
		if(flag == true)
		{
			//make the set of range
			vector<node> s(n);
			for(int j=0;j<n;j++)
			{
				s[j].l = v[j].first - sqrt(pow(d,2)-pow(v[j].second,2));
				s[j].r = v[j].first + sqrt(pow(d,2)-pow(v[j].second,2));
				//cout<<s[j].l<<' '<<s[j].r<<endl;	
			}
			//work out the public subset
			sort(s.begin(),s.end(),comp);
			cout<<"Case "<<i+1<<": "<<subsetn(s)<<endl;
		}
		else
		{
			//do nothing!
		}
	}

	return 0;
}


int subsetn(vector<node> s)
{
	vector<set<int> > record;
	vector<bool> visit(s.size(),false);
	
	for(int i=0;i<s.size();i++)
	{
		set<int> stemp;
		if(visit[i] == false)
		{
			visit[i] = true;
			stemp.insert(i);
			node ntemp = s[i];
			for(int j=0;j<s.size();j++)
			{
				if(visit[j]==false && isIntersect(ntemp,s[j]))
				{
					visit[j] = true;
					stemp.insert(j);
					ntemp = calIntersect(ntemp,s[j]);
				}
			}
			record.push_back(stemp);
		}
	}

	return record.size();
}

bool isIntersect(node a,node b)
{
	return a.r >= b.l && b.r >= a.l;
}

node calIntersect(node a,node b)
{
	node result;
	result.l = max(a.l,b.l);
	result.r = min(a.r,b.r);
	return result;
}


WPS JS宏实战:批量调整200个Word表格格式的避坑指南(附完整代码) 本文提供了一份详尽的WPS JS宏实战指南,针对批量调整大量Word表格格式的需求。文章深入解析了如何利用JS宏自动化设置字体、行距和边框,并重点解决了常见的“首行访问报错”等核心难题,附有可直接使用的完整代码,帮助用户显著提升文档处理效率。 阅读详情

相关推荐

波士顿咨询最新发布《2025年全球支付报告:未来不稳定》:到2029 年,支付收入将预计达到 2.4 万亿美元

波士顿咨询最新发布《2025年全球支付报告:未来不稳定》:到2029 年,支付收入将预计达到 2.4 万亿美元

face light~ 895

1328Radar Installation:如何贪心+坑点记录+一组样例数据

题目大意 假设海岸是无限的直线。 陆地在海岸的一侧,海洋在另一侧。 每个小岛都是位于海边的一个地点。 而且,位于海岸上的任何雷达装置只能覆盖d距离,因此,如果它们之间的距离最大为d,则可以用半径装置覆盖海中的一个岛。 我们使用笛卡尔坐标系,定义了惯性运动为x轴。 海洋一侧在x轴上方,陆地一侧在x轴上方。 给定海洋中每个岛的位置,并确定雷达装置覆盖的距离,您的任务是编写一个程序,以找到覆盖所有岛的最...

csyifanZhang的博客 619

新手如何学习车载底层嵌入式软件开发(AutoSar)

汽车嵌入式开发

ACarSoft的博客 4159

最大不相交子集 POJ1328

先解释一下最大不相交子集: 设有许多集合[Ai,Bi]; 最大不相交子集: 所有集合相交,产生的子集,这些产生的子集中的任意两个子集相交都为空集,产生的子集一定是最大的。 算法框架: for (int i ;i < n ;i++) { if (子集i的左边 > 子集j的右边 (i > j) ) { 最大不相交子集个数加一; 将j变为i; ...

weixin_44368514的博客 2986

POJ:1328 Radar Installation 解题思路(采用优先队列和栈实现)、注意点以及部分测试数据

POJ:1328 Radar Installation 解题思路(采用优先队列和栈实现)、注意点以及部分测试数据 本题选自PKU Online Judge(POJ)——http://poj.org/当中的第1328题 题目不难,但是比较经典,主要的思路是采用贪心算法求解,但是需要转化一下才行,网络上也有其他的博主写关于此题的思路,但是我这里主要是采用STL方式进行解题(priority_qu...

领头“洋” 700

【题解】POJ 1328 Radar Installation(贪心)

POJ 1328 Radar Installation 原题 假设滑行是一条无限长的直线。陆地在海岸的一边,大海在另一边。每个小岛都是位于海边的一个点。而任何定位在海岸上的雷达装置,只能覆盖d距离,所以海上的岛屿可以被半径装置覆盖,如果它们之间的距离最多是d。 我们使用笛卡尔坐标系,定义滑行是x轴。海面在x轴上方,陆地面在x轴下方。考虑到每个岛屿在海上的位置,以及雷达装置的覆盖距离,您的任务是编写一个程序来查找覆盖所有岛屿的雷达装置的最小数量。注意,一个岛的位置是由它的x-y坐标表示的。 图一雷达装置的样

bu_shushu的博客 1274

POJ1328 Radar Installation(区间贪心)

题目地址:http://poj.org/problem?id=1328 题目大意: 题意:多组数据输入,每组数据第一行给出N,R接着有N组小岛坐标,雷达半径为R。X轴以上是海洋,X轴以下是陆地,雷达只能部署到X轴上,输出覆盖海洋上N个小岛最少需要多少雷达,如果不能完全覆盖,输出-1; 思路:假设雷达在小岛上,那么雷达覆盖范围和X轴就有相交区间,记录下这个区间的左右极限,接着就是区间覆盖问题,...

qq_37618760的博客 238

POJ 1328 Radar Installation

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36401   Accepted: 8092 Description Assume the coasting is an infinite straight line. Land is

hqu_fritz的专栏 522

入门第三题 poj 1328 Radar Installation

题目链接http://poj.org/problem?id=1328 思路:贪心,问题的关键在于找到海岛的可放置的雷达区间,然后按照区间的左端点的大小利用sort进行结构体的升序排列。然后遍历整个数组看后一个区间的左端点是否小与前面一整段中最小的右端点,如果大于的话那么雷达数目就要+1并且忽视掉前面一整段建立起新的一段;如果小与的话并且右端点也小与前一整段中的最小右端点,那就更新最小右端点。

liujc_的专栏 579

POJ 1328 - Radar Installation

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, l

Dilthey's Blog 362

POJ 1328Radar Installation

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 50187   Accepted: 11257 Description Assume the coasting is an infinite straight line. Land

bobo 579

POJ 1328 Radar Installation贪心算法

先求出各个岛屿能被搜索到的放置雷达区间,排序(也可用优先队列),然后把存在重叠部分的区间算作1个雷达,最终得到的雷达个数即为正解,即用最少的点去遍布所有的区间。无解的情况即为某岛屿的纵坐标大于雷达的半径,因此在读入数据时即可作出判断。在一个平面直角坐标系上,有n个给出坐标的岛屿,问在X轴上至少需要放置多少个半径为d的雷达可以遍布所有岛屿?(若无法遍布所有岛屿则输出-1)

m0_72495985的博客 175

POJ 1328 Radar Installation(贪心)

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 64148   Accepted: 14422 Description Assume the coasting is an infini

Chunzhen的博客 513

POJ1328-Radar Installation

做了好几天的贪心题目,看见这道题的时候,想都没想。直接来个

KJBU2 684

poj 1328 Radar Installation

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 76213   Accepted: 17078 Description Assume the coasting is an infinite straight line. La

大众博客 293

POJ-1328 Radar Installation

Radar Installation 贪心算法,好难啊。

傲娇的晓明 310

(Relax 1.6)POJ 1328 Radar Installation(利用数据有序化进行贪心选择)

/* * POJ_1328.cpp * * Created on: 2013年11月18日 * Author: Administrator */ #include #include #include #include using namespace std; const int maxn = 1010; struct tt{ double l,r; }p[m

黄俊东的专栏 1205

1328:Radar Installation(贪心算法

查看 提交 统计 提示 提问 总时间限制:1000ms内存限制:65536kB描述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 s...

Liuxrfly的博客 90

POJ -1328 Radar Installation

假设陆地的海岸线是一条无限延长的直线,海岛是一个个的点,现需要在海岸线上安装雷达,使整个雷达系统能够覆盖到所有的海岛。雷达所能覆盖的区域是以雷达为圆心半径为d的圆,我们用指标坐标系来描述,海岸线就是x轴,现在给出每个海岛的坐标与雷达的半径d,请编写一个程序计算出最少需要多少个雷达才能够将所有海岛全部覆盖? 输入:                              输入将会有多组数据,每组数...

usernamezzz的博客 189
上一篇: C++:排列组合算法
下一篇: iperf3的最简教程
DeepMindMan
博客等级 码龄10年 12粉丝 5原创
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值