zoukankan      html  css  js  c++  java
  • POJ

    http://poj.org/problem?id=1113

    题意

    求能包围城堡的最小周长,其中必须与城堡每个点相隔L。

    分析

    答案是凸包周长加上一个圆周长,即包围凸包的一个圆角多边形。

    但那些圆角加起来为什么恰好是一个圆呢?每个圆角是以凸包对应的顶点为圆心,给定的L为半径,与相邻两条边的切点之间的一段圆弧。

    每个圆弧的两条半径夹角与对应的凸包的内角互补。假设凸包有n条边,则所有圆弧角之和为180°*n-180°*(n-2)=360°。(凸边形内角和为(n-2)*180)

    故,围墙周长为=n条平行于凸包的线段+n条圆弧的长度=凸包周长+围墙离城堡距离L为半径的圆周长。

    #include<stdio.h>
    #include<math.h>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    const int MAXN=1000;
    const double PI=acos(-1.0);
    
    struct point
    {
        int x,y;
    };
    point list[MAXN];
    int stack[MAXN],top;
    
    int cross(point p0,point p1,point p2) //计算叉积  p0p1 X p0p2 
    {
        return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
    }    
    double dis(point p1,point p2)  //计算 p1p2的 距离 
    {
        return sqrt((double)(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
    }    
    bool cmp(point p1,point p2) //极角排序函数 , 角度相同则距离小的在前面 
    {
        int tmp=cross(list[0],p1,p2);
        if(tmp>0) return true;
        else if(tmp==0&&dis(list[0],p1)<dis(list[0],p2)) return true;
        else return false;
    }    
    void init(int n) //输入,并把  最左下方的点放在 list[0]  。并且进行极角排序 
    {
        int i,k;
        point p0;
        scanf("%d%d",&list[0].x,&list[0].y);
        p0.x=list[0].x;
        p0.y=list[0].y;
        k=0;
        for(i=1;i<n;i++)
        {
            scanf("%d%d",&list[i].x,&list[i].y);
            if( (p0.y>list[i].y) || ((p0.y==list[i].y)&&(p0.x>list[i].x)) )
            {
                p0.x=list[i].x;
                p0.y=list[i].y;
                k=i;
            }    
        }    
        list[k]=list[0];
        list[0]=p0;
        
        sort(list+1,list+n,cmp);
    }     
    
    void graham(int n)
    {
        int i;
        if(n==1) {top=0;stack[0]=0;}
        if(n==2)
        {
            top=1;
            stack[0]=0;
            stack[1]=1;
        }    
        if(n>2)
        {
            for(i=0;i<=1;i++) stack[i]=i;
            top=1;
            
            for(i=2;i<n;i++)
            {
                while(top>0&&cross(list[stack[top-1]],list[stack[top]],list[i])<=0) top--;
                top++;
                stack[top]=i;
            }    
        }    
    }    
    
    int main()
    {
        int N,L;
        while(scanf("%d%d",&N,&L)!=EOF)
        {
            init(N);
            graham(N);
            double res=0;
            for(int i=0;i<top;i++)
              res+=dis(list[stack[i]],list[stack[i+1]]);
            res+=dis(list[stack[0]],list[stack[top]]);
            
            res+=2*PI*L;
            printf("%d
    ",(int)(res+0.5));
            
        }    
        return 0;
    }
  • 相关阅读:
    WKT转换工具terraformers
    关于微信公众号投票结果的通告
    个人博客03
    个人博客02
    个人博客01
    四则运算2
    学习进度条
    构建之法阅读笔记01
    错误随笔
    软件工程概论第一节
  • 原文地址:https://www.cnblogs.com/fht-litost/p/9350133.html
Copyright © 2011-2022 走看看