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

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. 
POJ:1328 Radar Installation 思路以及测试数据_第1张图片 
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

这道题乍一看还是很好做的,结果做了一天。主要是思路不对,考虑不周到。
下面是我的 错误思路以及反例的测试样例:
思路一:
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
#include
#include
#include
#include
#include
#include
using namespace std;

struct node
{
	float l;
	float r;	
};

bool comp(const node &a,const node &b)
{
	return a.l 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 >v(n);
		for(int j=0;j>v[j].first>>v[j].second;
		}
		//decide the condition.
		bool flag = true;
		if(d<0)
		{
			cout<<"Case "<d || v[j].second<0)
				{
					cout<<"Case "< s(n);
			for(int j=0;j s)
{
	vector > record;
	vector visit(s.size(),false);
	
	for(int i=0;i stemp;
		if(visit[i] == false)
		{
			visit[i] = true;
			stemp.insert(i);
			node ntemp = s[i];
			for(int j=0;j= 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;
}


你可能感兴趣的:(C++,POJ)