POJ 1328 Greedy algorithm

Source: Internet
Author: User
Tags radar

Test instructions: Use radar to cover all islands. Given the number of islands N and the radius of the radar d,n the coordinates of the line (x, y), first the radar interval is calculated and sorted. Then, from left to right, the answer adds 1 when you find that the starting point of the next interval is greater than the minimum end of all the previous intervals.

#include <iostream> #include <algorithm> #include <cmath> using namespace std;
const int maxn=1001;
int ans=0;
int n,d;
BOOL OK;
	struct Position {int x;
int y;
}POSITION[MAXN]; BOOL operator < (const Position &a,const Position &b) {return a.x<b.x;} bool operator = = (Const Position &A
	Mp;a,const Position &b) {return (a.x==b.x) && (A.Y==B.Y);} void init () {int i;
	Ok=true;
		for (i=0;i<n;i++) {cin>>position[i].x>>position[i].y;
		if (position[i].y>d) {ok=false;
	}}} double Getx (Position &position) {return position.x+sqrt (double (D*D-POSITION.Y*POSITION.Y));} void work () {
	Double x,temp;
	int i;
	X=getx (Position[0]);
	Ans=1;
		for (i=1;i<n;i++) {temp=getx (position[i]);
			if (x>temp&&x>position[i].x) {x=temp;
		Continue
		} if ((position[i].x-x) * (position[i].x-x) + (POSITION[I].Y*POSITION[I].Y) <=d*d) {continue;
		} x=getx (Position[i]);
	ans++; }} int main (int argc,Char *argv[]) {int casenum=0;
		while (Cin>>n>>d &&n!=0&&d!=0) {init ();
		casenum++;
			if (!ok) {cout<< "case" <<casenum<< ":-1" <<endl;
		Continue
		} sort (position,position+n);
		Work ();
	cout<< "Case" <<casenum<< ":" <<ans<<endl;
} return 0; }



 

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.