POJ 1228 Grandpa ' s Estate--deep understanding of convex hull

Source: Internet
Author: User

Test instructions: Determine whether the convex hull is stable.

Solution: There are at least three points on each edge of the stable convex hull.

The problem is to find the details of the convex hull, there are two algorithms for convex package:

1. The Andrew algorithm based on the horizontal order

2. The Graham algorithm based on the polar angle sequence

Both of these algorithms have a statement similar to the following:

for (int i=0;i<n;i++) {when        (M > 1 && Cross (Ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;        ch[m++] = P[i];    }

In this case, to find out is the simplest convex package, that is, the number of points as low as possible convex package, because cross = = 0 of the case is also out of the stack, so a convex side will be three points collinear.

Let's change the sentence and put the cross. <=0 changed into cross. < 0, then the most complex convex package, that is, a convex edge may contain many points also belong to the convex hull point.

That is, the following scenario:

The simplest convex package is a blue four point. The most complex bumps are all blue dots and red dots.

As this problem, we can actually ask:

1. In order to find the simplest convex hull, we only need to determine how many points in total are on the convex edge (the end is counted), if < 3, it does not match.

2. If the most complex convex hull, you can not use the above judgment, because the sentence is only two points, this time may use the following method:

Assuming that I want to judge the side I, then judge the side I and the side I-1, Edge I and the angle ofThe Edge i+1 is 0 (in).                                        ----Xdruid

Code: (I'm using the Andrew algorithm here)

#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>#defineEPS 1e-8using namespacestd;structpoint{Doublex, y; Point (Doublex=0,Doubley=0): X (x), Y (y) {}voidInput () {scanf ("%LF%LF",&x,&y); }};typedef point Vector;intDCMP (Doublex) {if(x <-eps)return-1; if(X > EPS)return 1; return 0;} Template<classT> T Sqr (t x) {returnX *x;} Vectoroperator+ (vector A, vector B) {returnVector (a.x + b.x, A.Y +b.y); }vectoroperator-(vector A, vector B) {returnVector (a.x-b.x, A.Y-b.y); }vectoroperator* (Vector A,DoubleP) {returnVector (A.x*p, a.y*p); }vectoroperator/(Vector A,DoubleP) {returnVector (a.x/p, a.y/p); }BOOL operator< (Constpoint& A,Constpoint& b) {returna.x < b.x | | (a.x = = b.x && A.y <b.y); }BOOL operator>= (Constpoint& A,Constpoint& b) {returna.x >= b.x && a.y >=b.y;}BOOL operator<= (Constpoint& A,Constpoint& b) {returna.x <= b.x && a.y <=b.y;}BOOL operator== (Constpoint& A,Constpoint& b) {returnDCMP (a.x-b.x) = =0&& dcmp (a.y-b.y) = =0; }DoubleDot (vector A, vector B) {returna.x*b.x + a.y*b.y;}DoubleLength (Vector A) {returnsqrt (Dot (A, a));}DoubleAngle (vector A, vector B) {returnACOs (Dot (A, B)/Length (a)/Length (B)); }DoubleCross (vector A, vector B) {returna.x*b.y-a.y*b.x;}DoubleAngle (Vector v) {returnatan2 (V.Y, v.x);}BOOLOnsegment (Point P, point A, point B) {//endpoint does not count    returnDCMP (Cross (a-p,b-p)) = =0&& dcmp (Dot (a-p,b-p)) <=0;}intConvexhull (point* p,intN, point*ch) {Sort (p,p+N); intm =0;  for(intI=0; i<n;i++) {         while(M >1&& Cross (ch[m-1]-ch[m-2], p[i]-ch[m-2]) <=0) m--; Ch[m++] =P[i]; }    intK =m;  for(inti=n-2; i>=0; i--) {         while(M > K && Cross (ch[m-1]-ch[m-2], p[i]-ch[m-2]) <=0) m--; Ch[m++] =P[i]; }    if(N >1) m--; returnm;} Point ch[1006],p[1006];intMain () {intt,n,i,j; scanf ("%d",&t);  while(t--) {scanf ("%d",&N);  for(i=0; i<n;i++) P[i].input (); if(N <=5) {puts ("NO");Continue; } intm =Convexhull (P,N,CH); if(M <=2) {puts ("NO");Continue; }  for(i=0; i<m;i++) {            intCNT =0;  for(j=0; j<n;j++)                if(Onsegment (p[j],ch[i],ch[(i+1)%m]) CNT++; if(CNT <3) Break; }        if(i = = m) puts ("YES"); ElsePuts"NO"); }    return 0;}
View Code

Now finally have a comprehensive understanding of their convex hull version, mother no longer have to worry about my use of the wrong convex bag. Ha ha.

POJ 1228 Grandpa ' s Estate--deep understanding of convex hull

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.