ZOJ-1360 Radar Installation

Radar Installation

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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.


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


先算出所有的島嶼可以覆蓋的區域,然後在交點上建立雷達,在向後面擴展結果時,選取重疊區域中右邊區域最小的作爲雷達的位置。


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

struct node {
	double x1;
	double x2;
};

void QuickSort(struct node *, int , int);
int process(struct node *, int, int);
int compare(struct node, struct node);

int main()
{
	int n, i, j, count = 0, total, flag;
	double dis, start, d, x, y;
	struct node pos[1000];
	while(scanf("%d%lf", &n, &d)) {
		count++;
		if(n == 0 && d == 0) {
			break;
		}
		flag = 1;
		for(i = 0; i < n; i++) {
			scanf("%lf%lf", &x, &y);
			if(flag) {
				dis = d*d - y*y;
				if(dis < 0) {
					flag = 0;
				} else {
					pos[i].x1 = x-sqrt(dis);
					pos[i].x2 = x+sqrt(dis);
				}
			}
		}
		printf("Case %d: ", count);
		if(flag == 0) {
			printf("-1\n");
		} else {
			QuickSort(pos, 0, n-1);
			total = 1;
			start = pos[0].x2;
			for(i = 1; i < n; i++) {
				if(pos[i].x1-start > 0) {
					total++;
					start = pos[i].x2;
				} else {
					if(pos[i].x2 < start) {
						start = pos[i].x2;
					}
				}
			}
			printf("%d\n", total);
		}
		getchar();
	}
	return 0;
}

void QuickSort(struct node *array, int s, int e)
{
	int k;
	if(s < e) {
		k = process(array, s, e);
		QuickSort(array, s, k-1);
		QuickSort(array, k+1, e);
	}
}

int process(struct node *array, int i, int j)
{
	struct node temp;
	temp = array[i];
	while(i < j) {
		while(i < j && compare(array[j], temp) >= 0) {
			j--;
		}
		array[i] = array[j];
		while(i < j && compare(array[i], temp) <= 0) {
			i++;
		}
		array[j] = array[i];
	}
	array[i] = temp;
	return i;
}

int compare(struct node n1, struct node n2)
{
	if(n1.x2 < n2.x2) {
		return -1;
	} else {
		return 1;
	}
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章