POJ 1328 Java Implementation

Source: Internet
Author: User
Tags radar

The main idea is to draw on other people's ideas, mainly as follows
/**
* For each island to be scanned, find out the range of radar that can be placed on the coast [Left,right], * Sort these intervals by left from small to large
* From the right point of the first interval, place the radar, then retrieve the next interval sequentially,
* If the next interval is completely within the range of the radar just placed (New.left>=pre.left &&
* new.right<=pre.right), the newly placed radar is relocated to the right end of the zone (new); * If the left end of the next interval is within the range of the radar, and the right end point is on the right side of the area where the radar has just been placed, this interval can be ignored;
* If the next interval is completely disjoint from the area where the radar has just been placed, place a new radar in the right end of the new zone */
The first commit did not pass, show Runtime_error, test a lot of data without errors, then the array size changed to 1000, passed smoothly.

1  Public classPointImplementsComparable<point> {2 3      PublicPoint () {4         //TODO auto-generated Constructor stub5 6     }7 8      Public DoubleLeft ;9      Public DoubleRight ;Ten  One     Private voidSetleft (DoubleLeft ) { A          This. left =Left ; -     } -  the     Private voidSetright (DoubleRight ) { -          This. right =Right ; -     } -  + @Override -      Public intCompareTo (Point O)//implementing Interfaces +     { A         if(O! =NULL&& oinstanceofPoint ) { atPoint P =(point) O; -             if(Left <p.left) -                 return-1; -             Else if(left = =p.left) -                 return0; -             Else in                 return1; -}Else to             return-1; +     } -  the      Public Static voidMain (string[] args) { *         //TODO auto-generated Method Stub $         intN, D;//number of islands and radar detection distancePanax NotoginsengPoint[] Poi =NewPOINT[100];//Store each island -Scanner sc =NewScanner (system.in); the         intnum = 1;//Case Number +         int[] Casenum =New int[100];//required number of radars per case An =sc.nextint (); theD =sc.nextint (); +          while(n! = 0 && D! = 0) { -             BooleanLegal =true;//determine if a case is legal, i.e. whether it can be covered by radar $             intx, y; $              for(inti = 0; I < n; i++) { -x =Integer.parseint (Sc.next ()); -y =Integer.parseint (Sc.next ()); thePoi[i] =NewPoint (); -                 //System.out.println (X-math.sqrt (d*d-y*y));Wuyi                 //double temp1=x-math.sqrt (d*d-y*y); thePoi[i].setleft (x-math.sqrt (d * d-y *y)); -Poi[i].setright (x + math.sqrt (d * d-y *y)); Wu                 if(Y >d) -Legal =false; About             } $Arrays.sort (POI, 0, N);//using the CompareTo of the above rewrite -             if(!legal) { -                 //System.out.println ("Case" +num+ ":-1"); -CASENUM[NUM-1] = 1; Anum++; +}Else { the                 DoublePre = Poi[0].right; -                 intRadar_num = 1; $                  for(inti = 1; I < n; i++) { the                     if(Poi[i].left >pre) { theradar_num++; thePre =Poi[i].right; the}Else if(Poi[i].right <pre) { -Pre =Poi[i].right; in                     } the                 } the                 //System.out.println ("Case" +num+ ":" +radar_num); AboutCASENUM[NUM-1] =Radar_num; thenum++; the             } then =sc.nextint (); +D =sc.nextint (); -  the         }Bayinum--; the          for(inti = 0; i < num; i++) { the             intj = i + 1; -SYSTEM.OUT.PRINTLN ("case" + j + ":" +casenum[i]); -         } the  the     } the  the}
View Code

POJ 1328 Java Implementation

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.