Radar Installation
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 57054 | Accepted: 12867 |
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
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
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
题目大意:
有n个岛屿在海上,雷达可以侦测的半径为r,求用最少的雷达覆盖所有的岛屿,如果不能覆盖到输出-1,否则输出最少的个数。
解题思路:
贪心思想:反过来我们可以以岛作半径为r的圆与海岸相交,求出交点,然后把交点当作区间,以区间左结点进行排序,如果后面的区间与前面的区间没有相交的部分,那么雷达数+1,如果下一个区间在当前区间的内部那么缩小区间可以覆盖更多,所以下一个区间的右结点可以作为新的区间结点,这样重复迭代可以完成本题。
代码如下:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn = 1002;
struct point
{
double start;
double end;
double x1,y1;
}a[maxn];
double b[maxn*2];
bool cmp(struct point a,struct point b)
{
return a.start < b.start;
}
int init(int m,int r)
{
int i,j;
int cnt;
for(i=0;i<m;i++)
{
cin>>a[i].x1>>a[i].y1;
}
for(i=0;i<m;i++)
{
if(a[i].y1 > r || r <= 0 || a[i].y1 <0)
{
return -1;
}
}
//求与x轴交点
for(i=0;i<m;i++)
{
b[2*i] = a[i].x1 - sqrt(1.0 * (r * r - a[i].y1 * a[i].y1));
b[2*i+1] = a[i].x1 + sqrt(1.0 * (r * r - a[i].y1 * a[i].y1));
//cout<<b[2*i]<<" "<<b[2*i+1]<<endl;
}
/*for(i=0;i<2*m;i++)
{
cout<<b[i]<<endl;
}*/
for(i=0;i<m;i++)
{
a[i].start = b[i*2];
a[i].end = b[i*2+1];
//cout<<a[i].start<<" "<<a[i].end<<endl;
}
//
sort(a,a+m,cmp);
cnt = 1;
double temp = a[0].end;
for(i=1;i<m;i++)
{
if(a[i].end<temp) temp=a[i].end;//如果下一个区间在当前区间的内部。
else if(a[i].start>temp)//如果下一个区间不在当前区间内。
{
cnt++;
temp=a[i].end;
}
}
/*for(i=0;i<m;i=j)
{
cnt++;
for(j=i+1;j<m;j++)
{
if(a[j].start > a[i].end)
break;
if(a[j].end < a[i].end)
{
a[i].end = a[j].end;
}
}
}*/
//cout<<a[0].start<<" "<<a[0].end<<endl;
return cnt;
}
int main()
{
int m,r;
//freopen("111","r",stdin);
int cnt1;
cnt1 = 0;
while(cin>>m>>r && !(m == 0 && r == 0))
{
cout<<"Case "<<++cnt1<<": "<<init(m,r)<<endl;
}
return 0;
}
运行结果: