`

poj 1113 wall (凸包问题)

阅读更多

Wall

Time Limit: 1000MS

 

Memory Limit: 10000K

Total Submissions: 17387

 

Accepted: 5627

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 100

200 400

300 400

300 300

400 300

400 400

500 400

500 200

350 200

200 200

Sample Output

1628

Hint

结果四舍五入就可以了

 

链接如下http://poj.org/problem?id=1113

 

题目大意:就是求凸包的周长+一个圆的周长。

就是一个典型求凸包的问题,下面是代码:

 

//求凸包问题
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
using namespace std;

const double PI = 3.14159265358979;
struct point
{
    double x,y;
    double thera;
}a[1005],chs[1005];

bool cmp1(point a,point b)    //寻找左下角的点
{
    if( a.y == b.y ) return a.x < b.x;
    return a.y < b.y;
}
bool cmp2(point a,point b)
{
    if( a.thera == b.thera ) //极角相同按x位置升序
        return a.x < b.x;
    else
        return a.thera < b.thera;
}
double get_thera(point a0,point a1)     //求两点直线与x轴的夹角
{
    return atan2((a1.y-a0.y),(a1.x-a0.x));
}
double dist_2point(point a1,point a2)   //两点距离
{
    return sqrt((a1.x-a2.x)*(a1.x-a2.x)+(a1.y-a2.y)*(a1.y-a2.y));
}

bool line_3point(point a1,point a2,point a3)
{
    double P,Q,M;
    //又是那个原理:求点在直线的左边还是右边。(叉乘!)
    P=(a2.x - a1.x) * (a3.y - a1.y);
    Q=(a3.x - a1.x) * (a2.y - a1.y);
    M=P-Q;
    if(M>0)  return false;  //负数 说明在向量的右边,是顺时针旋转的。
    if(M<0)  return true;   //正数 说明在向量的左边,是逆时针旋转的。
    if(M==0) return true;  //零   说明线段的直线上,则点(x3,y3)是共线。
	return false;
}

int main()
{
    int i,n,top;
	double r,len,shan,sum;
    while(scanf("%d%lf",&n,&r)!=EOF)
    {
        len=0;
        for(i=0;i<n;i++)
            scanf("%lf%lf",&a[i].x,&a[i].y);
        ///求左下角的点(以它未基准)  y最小,若相同,x要最小。
		sort(a,a+n,cmp1);

        ///下面就是极角排序
		for(i=0;i<n;i++)    //求两点连线与x轴夹角
		    a[i].thera=get_thera(a[0],a[i]);  //得到夹角的弧度
		sort(a+1,a+n,cmp2); //对夹角进行由小到大的排序

		///下面就是Wall Graham_Scan算法
        chs[0]=a[0]; chs[1]=a[1]; chs[2]=a[2];
        top=2;
        for(i=3;i<n;i++)
        {
            while(top>=1 && line_3point(chs[top-1],chs[top],a[i]))
            {
                top--;
            }
            chs[++top]=a[i];    //chs[]就是凸包的点
        }
        ///这样求出chs[]的点就是凸包的所有顶点!!!(逆时针)

        top++;  //小心注意!因为最后chs[++top]=a[i],所以要top++!
        for(i=0;i<top;i++)
            len+=dist_2point(chs[i%top],chs[(i+1)%top]);

        shan=2.0*PI*r;
        sum=shan+len;
		printf("%.0lf\n",sum);
    }
    return 0;
}
2
1
分享到:
评论
1 楼 基德KID.1412 2011-07-27  
灰常好,嘎嘎嘎

相关推荐

Global site tag (gtag.js) - Google Analytics