Main topic:
Given the n vertices of the polygon castle, build a fence around the castle, surround all the points, and the wall is at least as far from all points as L, seeking the smallest length of the wall.
Ideas:
The result equals the perimeter of the convex hull + the circumference of a complete circle.
When walking around the castle, the Radian formed on the corner is added to a circle with a length of L.
1 /*2 POJ 11133 Convex bag4 Effect:5 given the n vertices of a polygon castle, build a fence around the castle, surround all the points,6 and the distance between the wall and all points is at least L, to find the smallest length of the wall.7 */8 9#include <iostream>Ten#include <math.h> One#include <cstring> A#include <algorithm> -#include <stdlib.h> - the using namespacestd; - #definePi ACOs (-1.0) - #defineEPS 1e-8 - #defineZero (x) (((x) >0? ( x):-(x)) <eps) + - struct Point + { A Doublex, y; at}p[1005], convex[1005]; - - //distance between two points - DoubleDis (point A, point B) - { - returnsqrt ((a.x-b.x) * (a.x-b.x) + (A.Y-B.Y) * (A.Y-b.y)); in } - to //Calculate cross Product (P1-P0) x (p2-p0) + //Cross Product - DoubleXmult (Point P1, point P2, point p0) the { * return(p1.x-p0.x) * (P2.Y-P0.Y)-(p2.x-p0.x) * (P1.Y-p0.y); $ }Panax Notoginseng //The Graham algorithm constructs a convex hull with all collinear points clockwise, O (NLOGN) - Point P1, p2; the + intGRAHAM_CP (Const voidAConst void*b) A { the DoubleRET = Xmult (* ((point*) a), * ((point*) (b), p1); + DoubleRet2 = Xmult (* ((point*) a), * (point*) (b), p2); - returnZero (ret)? (ret2>0?1: -1): (Ret >0?1: -1); $ } $ - void_graham (intN, point* p,int& S, point*ch) - { the intI, k =0; - for(P1 = P2 = p[0], I =1; i<n; p2.x + = p[i].x, p2.y + = P[i].y, i++)Wuyi if(P1.y-p[i].y>eps | | (zero (P1.Y-P[I].Y) && p1.x >p[i].x)) theP1 = P[k =i]; -p2.x/= N, p2.y/=N; WuP[k] = p[0], p[0] =P1; -Qsort (P +1N1,sizeof(point), GRAHAM_CP); About for(ch[0] = p[0], ch[1] = p[1], ch[2] = p[2], s = i =3; I < n; ch[s++] = p[i++]) $ for(; s>2&& Xmult (Ch[s-2], P[i], Ch[s-1]) <-eps; s--); - } - - intWIPESAME_CP (Const void*a,Const void*b) A { + if((* (point *) a). Y < (* (point *) b). Y-eps)return-1; the Else if((* (point *) a). Y > (* (point *) b). Y + eps)return 1; - Else if((* (point *) a). x < (* (point *) b). X-eps)return-1; $ Else if((* (point *) a). x > (* (point *) b). x + EPS)return 1; the Else return 0; the } the the int_wipesame (Point * p,intN) - { in intI, K; theQsort (P, N,sizeof(point), WIPESAME_CP); the for(k = i =1; I < n; i++) About if(WIPESAME_CP (p + I, p + i-1) !=0) p[k++] =P[i]; the returnK; the } the + //constructs the convex hull interface function, passes in the original point set size n, the point set P (p original order is disturbed!) - //returns the convex hull size, convex hull points in convex the //parameter MaxSize 1 contains collinear points, 0 does not contain collinear points, default is 1Bayi //The parameter dir is 1 clockwise constructed, 0 counterclockwise, and defaults to 1 the //The algorithm is unstable when the input has only a few collinear points, there may be such a situation please deal with it separately! the intGrahamintN, point* p, point* convex,intMaxSize =1,intDIR =1) - { -point* temp =NewPoint[n]; the ints, I; then =_wipesame (P, n); the _graham (n, p, S, temp); the for(convex[0] = temp[0], n =1, i = (dir?1: (S-1)); Dir? (I < s): I; i + = (dir?)1: -1)) - if(MaxSize | |!zero (xmult (Temp[i-1], Temp[i], temp[(i +1)%S] ))) theconvex[n++] =Temp[i]; the Delete[]temp; the returnN;94 } the the intMain () the {98 intN; About DoubleD; - while(Cin >> N >>d)101 {102Memset (P,0,sizeof(P));103memset (Convex,0,sizeof(convex));104 DoubleDist =0.0; the for(inti =0; I < n; i++)106 {107CIN >> p[i].x >>p[i].y;108 }109 //cout << n << endl; the intsize = Graham (n, p, convex,0);111 //cout << size << Endl; the for(inti =0; I < size-1; i++)113 { theDist + = Dis (convex[i], convex[i +1]); the } theDist + = dis (convex[size-1], convex[0]);//don't forget the length from the beginning to the end117cout << (int) (Dist +2* pi * d +0.5) <<Endl;118 }119}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced
Poj 1113 Wall [convex bag]