POJ 1113 Wall(凸包 多边形周长)

来源:互联网 发布:php框架 知乎 编辑:程序博客网 时间:2024/05/16 16:17

Wall
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 29843 Accepted: 10027

Description

Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he would not listen to his Architect's proposals to build a beautiful brick wall with a perfect shape and nice tall towers. Instead, he ordered to build the wall around the whole castle using the least amount of stone and labor, but demanded that the wall should not come closer to the castle than a certain distance. If the King finds that the Architect has used more resources to build the wall than it was absolutely necessary to satisfy those requirements, then the Architect will loose his head. Moreover, he demanded Architect to introduce at once a plan of the wall listing the exact amount of resources that are needed to build the wall. 

Your task is to help poor Architect to save his head, by writing a program that will find the minimum possible length of the wall that he could build around the castle to satisfy King's requirements. 

The task is somewhat simplified by the fact, that the King's castle has a polygonal shape and is situated on a flat ground. The Architect has already established a Cartesian coordinate system and has precisely measured the coordinates of all castle's vertices in feet.

Input

The first line of the input file contains two integer numbers N and L separated by a space. N (3 <= N <= 1000) is the number of vertices in the King's castle, and L (1 <= L <= 1000) is the minimal number of feet that King allows for the wall to come close to the castle. 

Next N lines describe coordinates of castle's vertices in a clockwise order. Each line contains two integer numbers Xi and Yi separated by a space (-10000 <= Xi, Yi <= 10000) that represent the coordinates of ith vertex. All vertices are different and the sides of the castle do not intersect anywhere except for vertices.

Output

Write to the output file the single number that represents the minimal possible length of the wall in feet that could be built around the castle to satisfy King's requirements. You must present the integer number of feet to the King, because the floating numbers are not invented yet. However, you must round the result in such a way, that it is accurate to 8 inches (1 foot is equal to 12 inches), since the King will not tolerate larger error in the estimates.

Sample Input

9 100200 400300 400300 300400 300400 400500 400500 200350 200200 200

Sample Output

1628

Hint

结果四舍五入就可以了

Source

Northeastern Europe 2001

纪念自己做的第一个凸包,顺便把感觉特棒的一个模版贴出来。

题意:给一个城堡,要求在城堡的外围一圈建造围墙,对于城堡的每个点,围墙离他的最远距离都要是r

分析:首先求出城堡所有点的凸包,在往外扩r的距离,容易知道,对于变形后的图形,周长就等于远多边形周长加一个完整的圆。

代码如下:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <vector>using namespace std;const double PI = acos(-1.0);struct Point{    double x,y;    Point(double x=0,double y=0):x(x),y(y){}};typedef Point Vec;//向量+向量 = 向量,点+向量 = 点Vec operator +(Vec A,Vec B){return Vec(A.x+B.x,A.y+B.y);}//点-点 = 向量Vec operator -(Point A,Point B){return Vec(A.x-B.x,A.y-B.y);}//向量*数 = 向量Vec operator *(Vec A,double p){return Vec(A.x*p,A.y*p);}//向量/数 = 向量Vec operator /(Vec A,double p){return Vec(A.x/p,A.y/p);}bool operator <(const Point& a,const Point& b){    return a.x<b.x || (a.x == b.x && a.y<b.y);}const double EPS = 1e-10;int dcmp(double x){    if(fabs(x)<EPS) return 0;else return x<0? -1: 1;}bool operator == (const Point& a,const Point &b){    return dcmp(a.x-b.x)==0 &&dcmp(a.y-b.y) == 0;}/*==========以上为基本定义============*/double Dot(Vec A,Vec B){return A.x*B.x+A.y*B.y;}double Length(Vec A){ return sqrt(Dot(A,A));}double Angle(Vec A,Vec B){return acos(Dot(A,B)/Length(A)/Length(B));}/*==========用点积算向量长度和两个向量夹角============*/double Cross(Vec A,Vec B){return A.x*B.y - A.y*B.x;}//ABC的三角形有向面积的两倍double Area2(Point A,Point B,Point C){return Cross(B-A,C-A);}//rad是弧度 逆时针旋转Vec Rotate(Vec A,double rad){    return Vec(A.x*cos(rad)-A.y*sin(rad),A.x*sin(rad)+A.y*cos(rad));}//逆时针旋转90°的单位法向量Vec Normal(Vec A){    double L = Length(A);    return Vec(-A.y/L,A.x/L);}/*=====以上为叉积的基本运算=====*///P+tv和Q+tw两条直线的交点,确保有唯一交点Point GetlineIntersection(Point P,Vec v,Point Q,Vec w){    Vec u = P-Q;    double t = Cross(w,u)/Cross(v,w);    return P+v*t;}//点到直线的距离double DistanceToLine(Point P,Point A,Point B){    Vec v1 = B-A,v2 = P-A;    return fabs(Cross(v1,v2)/Length(v1));//不取绝对值那么得到的是有向距离}//点到线段的距离double DistanceToSegment(Point P,Point A,Point B){    if(A == B)return Length(P-A);    Vec v1 = B - A, v2 = P - A, v3 = P - B;    if(dcmp(Dot(v1,v2))<0)return Length(v2);    else if(dcmp(Dot(v1,v3)>0)) return Length(v3);    else return fabs(Cross(v1,v2))/Length(v1);}//点在直线上的投影Point GetLineProjection(Point P,Point A,Point B){    Vec v = B - A;    return A + v*(Dot(v,P-A) / Dot(v,v));}//判断两条线段是否相交 此处必须为规范相交bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2){    double c1 = Cross(a2-a1,b1-a1),c2 = Cross(a2-a1,b2-a1);    double c3 = Cross(b2-b1,a1-b1),c4 = Cross(b2-b1,a2-b1);    return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0;}//如果允许端点相交,则用以下代码,判断一个点是否在一条线段上bool OnSegment(Point p,Point a1,Point a2){    return dcmp(Cross(a1-p,a2-p)) == 0 && dcmp(Dot(a1-p,a2-p))<0;}/*=========以上为点和直线,直线和直线关系的内容========*///多边形有向面积double PolygonArea(Point* p,int n){    double area = 0;    for(int i=1;i<n-1;i++)        area += Cross(p[i] - p[0],p[i+1]-p[0]);    return area/2;}//判断一个浮点数是否为整数double isint(double x){    return fabs(x - (int)(x+0.5))<EPS;}//凸包vector<Point> ConvexHull(vector<Point> p){    sort(p.begin(),p.end());    //删除重复点    p.erase(unique(p.begin(),p.end()),p.end());    int n = p.size();    vector<Point> ch(n+1);    int m = 0;    for(int i=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];    }    int k = m;    for(int i = 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--;    ch.resize(m);    return ch;}double PolygonZhouc(vector<Point> p){    int n = p.size();    double ans = 0;    for(int i=0;i<n-1;i++)        ans+= Length(p[i+1]-p[i]);    ans+=Length(p[0]-p[n-1]);    return ans;}int main(){    int n,l;    while(scanf("%d%d",&n,&l)==2){        double x,y;        vector<Point> p,ans;        for(int i=1;i<=n;i++){            scanf("%lf%lf",&x,&y);            p.push_back(Point(x,y));        }        ans = ConvexHull(p);        double ret = PolygonZhouc(ans);        ret += 2*PI*l;        printf("%.0lf\n",ret);    }    return 0;}


0 0
原创粉丝点击