POJ 1113 Wall

做完2187之后这1113立马简单了啊。


题目大意:

给出一个多边形(不一定是凸多边形),求围住这个多边形且离多边形上每一点的最小距离是L的多边形的距离。




解题思路:

其实看这个题目中的图就能够明白这个题该怎么计算了。

1、先求这个多边形的凸包(用的Graham扫描法)。

2、求凸包上相邻点之间的距离。这样所有直的墙面的长度就求出来了。

3、再加上2*pi*L。因为所有转角角度之和一定是360度,所以要加一个圆的周长。


下面是代码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct node
{
    int x,y;
} point[1005],conhull[1005];
int cmp(const void *a,const void *b)
{
    struct node *aa=(struct node *)a;
    struct node *bb=(struct node *)b;
    if(aa->y==bb->y)return aa->x-bb->x;
    return aa->y-bb->y;
}
int n,cnt;
int xmult(node a,node b,node c)
{
    return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
}
void Graham()
{
    qsort(point,n,sizeof(point[0]),cmp);
    conhull[0]=point[0];
    conhull[1]=point[1];
    cnt=2;
    for(int i=2; i<n; i++)
    {
        while(cnt>1&&xmult(conhull[cnt-1],point[i],conhull[cnt-2])<=0)
        {
            cnt--;
        }
        conhull[cnt]=point[i];
        cnt++;
    }
    int min1=cnt;
    for(int i=n-2; i>=0; i--)
    {
        while(cnt>min1&&xmult(conhull[cnt-1],point[i],conhull[cnt-2])<=0)
        {
            cnt--;
        }
        conhull[cnt]=point[i];
        cnt++;
    }
}
double dist(node a,node b)
{
    return sqrt((double)((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y)));
}
int main()
{
    int l;
    double sum=0;
    scanf("%d%d",&n,&l);
    for(int i=0; i<n; i++)
    {
        scanf("%d%d",&point[i].x,&point[i].y);
    }
    Graham();
    for(int i=0; i<cnt-1; i++)
    {
       sum+=dist(conhull[i],conhull[i+1]);
    }
    sum+=2*3.141592654*l;
    printf("%.0f\n",sum);
    return 0;
}


posted @ 2014-01-06 19:28  、小呆  阅读(171)  评论(0编辑  收藏  举报