[poj1113&poj1696] convex hull roll wrapping algorithm and Graham scanning method applied each case

Source: Internet
Author: User

  Convex hull algorithm comparison image good understanding code writing is also relatively short, so before the exam should be no problem. >_<

POJ1113

  At first, I didn't understand why we had to use convex hull, and thought it would be better to stick to the castle.

The shortest perimeter in the case of a requirement to include all points in the subject is suddenly found. Is this the nature of the convex hull?

And obviously if the castle is concave, a circle inside will surely increase the perimeter ...

You can then generalize from the simple triangular quadrilateral, and find each corner-around 90 degrees after all the plus and 180 degrees.

That is, the length of the circumference of the castle plus a circle with a radius of L is the answer.

The last template was incorrectly written ... Should be the fast-line part did not deal well ... This time of re-writing only found ...

Program poj1113;ConstPi=3.14159265358979; maxn=1010; type point=record x,y:longint;end;varI,n,l,len:longint;     ans:extended; a:array[-1..     Maxn]of Point; dis:array[-1..     Maxn]of Int64; stack:array[-1.. Maxn]of point;function Getdis (a,b:point): Int64;begin exit (SQR ( a.x-b.x) +sqr (a.y-b.y)); End;function Cross (p0,p1,p2:point): Int64;begin exit ((p1.x-p0.x) * (P2.Y-P0.Y)-(p2.x-p0.x) * (p1.y-p0.y)); End;procedure qsort (l,r:longint);varI,j,mid:longint;      Midy:int64; Midx:point;begin I:=l;j:=r;mid:=random (r-l+1)+L; MIDX:=a[mid];midy:=Dis[mid]; Repeat while(I<r) and (Cross (a[1],A[I],MIDX) >0) or (Cross (a[1],A[I],MIDX) =0) and (Dis[i]<midy))) DoInc (I);  while(L&LT;J) and (Cross (a[1],A[J],MIDX) <0) or (Cross (a[1],A[J],MIDX) =0) and (Dis[j]>midy))) DoDec (j); ifi<=J THEN begin a[0]:=a[i];a[i]:=a[j];a[j]:=a[0]; dis[0]:=dis[i];d is[i]:=dis[j];d is[j]:=dis[0]; //The previous template here of the DIS array is not exchanged, the problem is the plane farthest point of the right ... It leads to a wrong point. Inc (I);d EC (j);    End Until I>J; ifi<R then Qsort (i,r); ifl<J then Qsort (l,j); end;procedure Graham (n:longint;varlen:longint);vark,i:longint;begin K:=0; a[0].x:=maxlongint;a[0].y:=Maxlongint;  fori:=1to n Do     if(A[I].Y&LT;A[K].Y) or ((A[I].Y=A[K].Y) and (a[i].x<a[k].x)) then k:=i; a[0]:=a[1];a[1]:=a[k];a[k]:=a[0];  fori:=2to n DoDis[i]:=getdis (a[i],a[1]); Qsort (2, N); Len:=3; stack[1]:=a[1];stack[2]:=a[2];stack[3]:=a[3];  fori:=4to n Dobegin whileCross (stack[len-1],a[i],stack[len]) >=0  DoDec (len); //If you ask for a point on the edge, you can't add an equal sign .Inc (LEN); stack[len]:=A[i]; End;end;begin//assign (input, ' poj1113.in '); reset (input); readln (n,l);  fori:=1to n Doreadln (A[I].X,A[I].Y);    Graham (N,len); Ans:=sqrt (Getdis (stack[1],stack[len])); //Cumulative Perimeter You cannot forget the edge between the first point and the nth point//Although it is easy to check out here, I hope the next cumulative length will not be forgotten. >_<     fori:=2To Len DoAns:=ans+sqrt (Getdis (stack[i],stack[i-1])); Writeln (ans+2*pi*l:0:0); end.

POJ1696

  How difficult life is ...

It seems that the students in my evaluation after the impact of the CE ... Qaq

This problem as long as the condition of the current point to the right of the point, of course, to ensure that the current point right turn no not taken.

First of all, it must be taken, and then wrapped in a volume to take it all the way ...

The only thing that needs to be modified is to mark the points you have taken, and then to easily ac ~

Program poj1696;Constmaxn= -; type point=record x,y,id:longint;end;varT,test,i,n:longint; a:array[-1..      Maxn]of Point; vis:array[-1..      Maxn]of Boolean; S:point;function Cross (p0,p1,p2:point): Longint;begin exit ((p1.x-p0.x) * (P2.Y-P0.Y)-(p2.x-p0.x) * (p1.y-p0.y)); End;function Getdis (a,b:point): Longint;begin exit (SQR (a.x-b.x) +sqr (a.y-b.y)); End;procedure Wrap (s:point);varI,j,k:longint;begin Fillchar (Vis,sizeof(VIS),true); Write (n,' ');  fori:=1to n Dobegin forj:=1to n Do ifVIS[J] THEN begin k:=j; Break; end; //just started too NAIVE ... The initial value of K is assigned to 0, then 0 nodes have a (0,1000), and later found that this satisfies the first condition Qaq//in order to maintain world peace ... Or try not to use the points that have not appeared ...         forj:=1to n Do if(Vis[j]) and (Cross (S,A[J],A[K) >0) or ((Cross (s,a[j],a[k]) =0) and (Getdis (a[j],s) <getdis (a[k],s))) then k:=J; Write (A[k].id,' '); s:=a[k];vis[k]:=false; End;end;begin//assign (input, ' poj1696.in '); reset (input);readln (test);  fort:=1To test Dobegin READLN (n); S.x:=0; s.y:=Maxlongint;  fori:=1to n Dobegin READLN (A[I].ID,A[I].X,A[I].Y); ifA[i].y<s.y then s.y:=a[i].y;        End    Wrap (s); writeln; End;end.

[poj1113&poj1696] convex hull roll wrapping algorithm and Graham scanning method applied each case

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.