POJ1328-Radar Installation
描述:
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
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?
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.
代碼:
每一個海島可以在產(chǎn)生一個圓心的范圍,在該范圍內(nèi)的任意雷達都可以覆蓋到該點。要求雷達的最少的數(shù)目,采用貪心的思路,就是使選擇的圓心能夠盡量的屬于更多的圓心的區(qū)間。如果雷達范圍無法到達某個點,則無解。
邊輸入邊處理的時候,不能break。就因為這個RE了無數(shù)次(╯‵□′)╯︵┴─┴
#include <cmath> #include <cstdlib> #include <iostream> using namespace std; typedef struct{double left;double right; }node; node a[1005]; int cmp(const void *a, const void *b){return (*(node*)a).left >= (*(node*)b).left ? 1 : -1; } int main(){int tc=1,n,d,flag,count,x,y;double delta,t;while( scanf("%d%d",&n,&d)!=EOF ){if( n==0 && d==0 ) break;flag=1;if( d<=0 ) flag=0;for( int i=0;i<n;i++ ){scanf("%d%d",&x,&y);if( y<=d ){//島嶼在雷達范圍delta=sqrt((double)(d*d-y*y));a[i].left=x-delta;//得到區(qū)間a[i].right=x+delta;}else{flag=0;//這里不能寫break,因為輸入還未結束 }}if( flag==0 )//無解printf("Case %d: -1\n",tc);else{qsort(a,n,sizeof(node),cmp); t=a[0].right;count=1;for( int i=1;i<n;i++ ){if( a[i].left>t ){//當前區(qū)間左界大于相交區(qū)間的最右count++;t=a[i].right;//更新相交右界 }else{if( a[i].right<t )//取相交的區(qū)間t=a[i].right;//更新相交區(qū)間右界 }}printf("Case %d: %d\n",tc,count);}tc++;}system("pause");return 0; }
轉載于:https://www.cnblogs.com/lucio_yz/p/4771828.html
總結
以上是生活随笔為你收集整理的POJ1328-Radar Installation的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设计模式之*
- 下一篇: 【转】Linux操作系统下/etc/ho