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.
![](https://i-blog.csdnimg.cn/blog_migrate/f6ffe515205096387436c13c7449b0ed.jpeg)
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
岛屿y大于雷达搜索半径,则所有雷达都无法搜索到该岛屿,输出结果为-1,将岛屿左右可以被雷达搜索到的距离求写出来,
再将岛屿按照左边距离的大小进行排序,例如若岛屿a[0]的右边边界都是小于a[1]的左边边界,————( )————
则说明a[0]独立自己被一个雷达搜索,而a[1]作为下一个点的判断条件,另一种情况是a[0]的右边界大于a[1]的右边界,————==————
==代表a[1]a[0]被雷达搜索的范围,——————代表a[0]被雷达搜索的范围
则说明a[1]被雷达搜索到的范围更小,为了使a[0]a[1]都被搜索到并进行下一步判断,则a[1]作为下一个岛屿的判断条件
#include
#include
#include
#include
#include
#include
#define maxn 1010 int n; double d; using namespace std; //可以在海岸线上搜索到小岛的左右距离 struct node { double left,right; }a[maxn],temp; //厉害了我的哥=.=c语言库里面排序不知道排左还是右。在这里重载一下< bool operator < (node a, node b) { return a.left < b.left; } int main() { int cas = 0; while(~scanf("%d%lf", &n, &d)) { int flag = 0; if(n == 0 && d == 0) break; cas++; for(int i = 0; i < n; i++) { double c,b; scanf("%lf%lf", &c, &b); //所有雷达都扫描不到 if(fabs(b) > d) flag = 1; else { a[i].left = c * 1.0 - sqrt(d * d - b * b); a[i].right = c * 1.0 + sqrt(d * d - b * b); } } printf("Case %d: ", cas); if(flag) printf("-1\n"); else { int num = 1; //从小到大排序 sort(a, a + n); temp = a[0]; for(int i = 1; i < n; i++) { //—————==——————交集部分可以放雷达 if(a[i].left > temp.right) { num += 1; temp = a[i]; } //若下一个岛屿能扫到的雷达区域小于上一个,则以小的为准 else if(a[i].right < temp.right) { temp = a[i]; } } printf("%d\n", num); } } return 0; }