[POJ1113] 墙

来源:互联网 发布:php 隐藏url真实路径 编辑:程序博客网 时间:2024/04/28 10:31

题目描述

一个贪婪的国王要求他的建筑师建一堵墙(图中虚线)围绕他的城堡(图中实线),且墙与城堡之间的距离总不小于一个数L。输入城堡各个节点(图中实线与实线的交点)的坐标和L,要求最小的墙壁周长。
这里写图片描述


输入格式

输入文件第一行N(3 <= N <= 1000)和L(1 <= L <= 1000),其中N为节点个数。
以下N行每行是各个节点的横坐标Xi和纵坐标Yi,其中-10000 <= Xi,Yi <= 10000。


输出格式

输出文件仅一个数,为最小的墙壁周长(四舍五入至整数)。


样例数据

样例输入

9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200

样例输出

1628


题目分析

凸包长度+半径为l的圆周长


源代码

#include<algorithm>#include<iostream>#include<iomanip>#include<cstring>#include<cstdlib>#include<vector>#include<cstdio>#include<cmath>#include<queue>using namespace std;inline const int Get_Int() {    int num=0,bj=1;    char x=getchar();    while(x<'0'||x>'9') {        if(x=='-')bj=-1;        x=getchar();    }    while(x>='0'&&x<='9') {        num=num*10+x-'0';        x=getchar();    }    return num*bj;}const double eps=1e-10;int DoubleCompare(double x) { //精度三出口判断与0关系    if(fabs(x)<eps)return 0; //=0    else if(x<0)return -1; //<0    else return 1; //>0}struct Point {    double x,y;    bool operator < (Point b) const {        return x<b.x||(x==b.x&&y<b.y);    }};double Dist(Point a,Point b) {    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}struct Vector {    double x,y;};Vector operator - (Point a,Point b) {    Vector c;    c.x=b.x-a.x;    c.y=b.y-a.y;    return c;}double Cross(Vector a,Vector b) { //叉积    return a.x*b.y-b.x*a.y;}double Area(Point a,Point b,Point c) { //三点的平行四边形有向面积    Vector u=b-a;    Vector v=c-a;    return Cross(u,v);}double Area(int n,Point* P) { //计算多边形有向面积(剖分法)     double ans=0;    for(int i=2; i<n; i++)ans+=Area(P[1],P[i],P[i+1]);    return ans/2;}int ConvexHull(int n,Point* p,Point* ans) { //Andrew算法求凸包(求上链与下链):p是点数组,ch是凸包顶点,返回顶点数     //输入不能有重复点,若要凸包边上没有输入点,将两个<=改为<    sort(p+1,p+n+1);    int top=0;    for(int i=1; i<=n; i++) {        while(top>1&&DoubleCompare(Cross(ans[top-1]-ans[top-2],p[i]-ans[top-2]))<=0)top--;        ans[top++]=p[i];    }    int k=top;    for(int i=n-1; i>=1; i--) {        while(top>k&&DoubleCompare(Cross(ans[top-1]-ans[top-2],p[i]-ans[top-2]))<=0)top--;        ans[top++]=p[i];    }    if(n>1)top--;    return top;}//////////////Point a[30005],b[30005];int n;double l,r;int main() {    n=Get_Int();    scanf("%lf",&r);    for(int i=1; i<=n; i++)scanf("%lf%lf",&a[i].x,&a[i].y);    int cnt=ConvexHull(n,a,b);    double s=Area(cnt,b);    for(int i=1; i<cnt; i++)l+=Dist(b[i],b[i+1]);    l+=Dist(b[cnt],b[1]);    if(s==0)l/=2;    printf("%d\n",int((l+2*3.1415926*r)+0.5));    return 0;}

0 0
原创粉丝点击