The plane convex package Graham algorithm

Source: Internet
Author: User

Board question Hdu1348wall

Plane convex hull problem is a classical problem in computational geometry
The specific point is to give a number of points on the plane, to find a minimum convex polygon, so that it contains all the points

The concrete image is similar to the plane has a number of pillars, a person with a rope from the periphery to tightly wrap it around a circle

Graham algorithm

Direct Talk algorithm
We will sort out all the points, respectively, to find the upper convex shell and the lower convex shell, together it is convex bag
Above the convex hull for example, we first add the leftmost point to the convex hull "as you can imagine, the leftmost point must be on the convex hull."
Then look backwards:
1, if there is only one point in the current convex hull, then add a new point
2. If there is more than one point in the current convex hull, check the relationship between the newly added point and the last point of the convex hull in the straight line with the last two points of the current convex hull.
If this is the case:

Satisfies the convexity, joins the convex bag
But if this is the case:

Not satisfied with the convexity, do not join
Specific judgment can be used with the slope can also be used to cross the "I write slope mad WA, or cross by comparison of AIDS"

Sweep over, you can get the upper convex hull, the lower convex shell is similar

Complexity \ (O (NLOGN) \)

hdu1348

#include <iostream>#include <cmath>#include <cstdio>#include <cstring>#include <algorithm>#define LL Long Long int#define REP (i,n) for (int i = 1; I <= (n); i++)using namespaceStdConst intMAXN =2005, MAXM =100005;Const DoubleINF =1000000000000000000LL;inline intRead () {intout =0, flag =1;Charc = GetChar (); while(C < -|| C > $) {if(c = ='-') flag =-1; c = GetChar ();} while(c >= -&& C <= $) {out = (out <<3) + (out <<1) + C-' 0 '; c = GetChar ();}returnOut * FLAG;}Const DoublePI = ACOs (-1);structpoint{Doublex, y;} P[MAXN];inline BOOL operator< (Constpoint& A,Constpoint& b) {returna.x = = b.x? A.y < b.y:a.x < b.x;}inlinePointoperator-(Constpoint& A,Constpoint& b) {return(point) {A.X-B.X,A.Y-B.Y};}inline Double operator*(Constpoint& A,Constpoint& b) {returna.x * B.Y-A.Y * b.x;}inline DoubleDisintUintV) {returnsqrt ((p[u].x-p[v].x) * (p[u].x-p[v].x) + (P[U].Y-P[V].Y) * (P[U].Y-P[V].Y));intT,n,l,q[maxn],tail;voidCal () {sort (P +1, p +1+ N); Q[tail =1] =1; for(inti =2; I <= N; i++) { while(Tail >1&& (P[i]-p[q[tail]) * (P[q[tail])-P[q[tail-1]]) <0) tail--;    Q[++tail] = i; }intlast = tail; for(inti = n-1; I i--) { while(Tail > Last && (P[i]-p[q[tail]]) * (P[q[tail])-P[q[tail-1]]) <0) tail--;    Q[++tail] = i; }}voidPrint () {DoubleAns =0; for(inti =1; i < tail; i++) ans + = dis (q[i],q[i +1]); Ans + =2.0* pi * L;if(T) printf ("%.0lf\n\n", ans);Elseprintf"%.0lf\n", ans);}intMain () {T = read (); while(t--) {n = read (); L = Read (); for(inti =1; I <= N;        i++) p[i].x = Read (), p[i].y = Read ();        Cal ();    Print (); }return 0;}

The plane convex package Graham algorithm

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.