zoukankan      html  css  js  c++  java
  • POJ 1113 Wall (凸包)

    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
    Sample Input
    9 100
    200 400
    300 400
    300 300
    400 300
    400 400
    500 400
    500 200
    350 200
    200 200
    Sample Output
    1628

    题意:

      要将所有点围成一个类似于矩形的形状,角变成弧。

      也就是求凸包周长+一个完整的圆周长。

      因为走一圈,经过拐点时,所形成的扇形的内角和是360度,故一个完整的圆。

      剩余的部分就是凸包的周长。

      具体如下图。

    思路:

      先求出凸包,然后求一个周长。注意四舍五入!!!

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #define PI 3.1415926535
    using namespace std;
    struct node
    {
        int x,y;
    };
    node vex[1000];//存入的所有的点
    node stackk[1000];//凸包中所有的点
    bool cmp1(node a,node b)
    {
        if(a.y==b.y)
            return a.x<b.x;
        else
            return a.y<b.y;
    }
    int cross(node a,node b,node c)//计算叉积
    {
        return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
    }
    double dis(node a,node b)//计算距离
    {
        return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y));
    }
    bool cmp(node a,node b)//极角排序
    {
        int m=cross(vex[0],a,b);
        if(m==0)
            return dis(vex[0],a)-dis(vex[0],b)<=0?true:false;
        else
            return m>0?true:false;
    }
    int main()
    {
        int t,L;
        while(~scanf("%d%d",&t,&L))
        {
            int i;
            for(i=0; i<t; i++)
            {
                scanf("%d%d",&vex[i].x,&vex[i].y);
            }
            if(t==1)
                printf("%.2f
    ",0.00);
            else if(t==2)
                printf("%.2f
    ",dis(vex[0],vex[1]));
            else
            {
                sort(vex,vex+t,cmp1);
                sort(vex+1,vex+t,cmp);
                memset(stackk,0,sizeof(stackk));
                stackk[0]=vex[0];
                stackk[1]=vex[1];
                int top=1;//最后凸包中拥有点的个数
                for(i=2; i<t; i++)
                {
                    while(i>=1&&cross(stackk[top-1],stackk[top],vex[i])<0)  
                        top--;
                    stackk[++top]=vex[i];                               
                }
                double s=0;//注意精度
                for(i=1; i<=top; i++)
                    s+=dis(stackk[i-1],stackk[i]);
                s+=dis(stackk[top],vex[0]);
                s+=2*PI*L;
                int ans=s+0.5;//四舍五入
                printf("%d
    ",ans);
            }
        }
    }
  • 相关阅读:
    php实现简单的流程管理
    【百度地图API】如何制作多途经点的线路导航——驾车篇
    利用MFC实现浏览器的定制与扩展(JavaScript与C++交互)
    c++与js脚本交互,C++调用JS函数/JS调用C++函数
    VC/MFC中通过CWebPage类调用javascript函数(给js函数传参,并取得返回值)
    Android中半透明Activity效果另法
    mac java环境
    在Mac osx使用ADT Bundle踩过的坑
    Android自动检测版本及自动升级
    C++编译遇到参数错误(cannot convert parameter * from 'const char [**]' to 'LPCWSTR')
  • 原文地址:https://www.cnblogs.com/aiguona/p/7248633.html
Copyright © 2011-2022 走看看