Poj1328-radar Installation

Source: Internet
Author: User
Tags radar

Describe:

Assume the coasting is a infinite straight line. Side of coasting, sea in the other. Each of small island was a point locating in the sea side. and any radar installation, locating on the coasting, can only cover D-distance, so a island in the sea can is 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 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 a 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 all case contains-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 was followed by n lines each containing and 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 all test case output one line consisting of the test case number followed by the minimal number of radar installation S needed. "-1" installation means no solution for this case.

Code:

Each island can have a range of centers, and any radar within that range can reach that point. The minimum number of requests for radar, the use of greedy ideas, is to make the center of choice as far as possible belong to more of the center of the range. If the radar range cannot reach a point, there is no solution.

You cannot break when you are working with the input side. Just because this re is not several times (╯‵-′) ╯︵┴─┴

#include <cmath>#include<cstdlib>#include<iostream>using namespaceStd;typedefstruct{    DoubleLeft ; DoubleRight ;} Node;node a[1005];intcmpConst void*a,Const void*b) {    return(* (node*) a). Left >= (* (node*) b). Left?1: -1;}intMain () {intTc=1, N,d,flag,count,x,y; Doubledelta,t;  while(SCANF ("%d%d", &n,&d)! =EOF) {        if(n==0&& d==0) Break; Flag=1; if(d<=0) flag=0;  for(intI=0; i<n;i++) {scanf ("%d%d",&x,&y); if(Y<=d) {//Islands in the radar rangeDelta=sqrt ((Double) (d*d-y*y)); A[i].left=x-delta;//Get the intervala[i].right=x+Delta; }            Else{flag=0;//Break cannot be written here because the input is not finished            }        }        if(flag==0)//No solutionprintf"Case %d: -1\n", TC); Else{qsort (a,n,sizeof(node), CMP); T=a[0].right;count=1;  for(intI=1; i<n;i++ ){                if(a[i].left>t) {//the left boundary of the current interval is greater than the right of the intersecting intervalcount++; T=a[i].right;//update intersect right boundary                }                Else{                    if(a[i].right<t)//to take the intersecting intervalT=a[i].right;//Update the right boundary of the Intersect interval}} printf ("Case %d:%d\n", Tc,count); } TC++; } System ("Pause"); return 0;}

  

Poj1328-radar Installation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.