HDU1348-Walls(经典凸包)

来源:互联网 发布:地图数据采集工具 编辑:程序博客网 时间:2024/06/05 04:32

Wall

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2220    Accepted Submission(s): 607


Problem 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.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.
 

Sample Input
19 100200 400300 400300 300400 300400 400500 400500 200350 200200 200
 

Sample Output
1628
 
题目大意:任意给你一个多边形,要求在多边形的外围建一堵围墙,要求是用的石头最少,并且多边形离墙体的距离不能大于给定的L,求外围墙的最少长度。
分析:最近学习凸包,刚开始用atan2排序,然后看了大牛的代码,不用atan2排序,直接就排了,写了两种代码,莫名的WA,求指点。。。
代码一:
#include<stdio.h>#include<stdlib.h>#include<math.h>#define PI 3.1415927struct point{    double x;    double y;}p[1100],res[1100],temp;double dis(struct point a, struct point b){    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}double multi(struct point p0, struct point p1,struct point p2){    return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);}int cmp(const void *a, const void *b) {    struct point A=*(struct point *)a;    struct point B=*(struct point *)b;    double k=multi(p[0],A,B);    if(k<0||!k&&dis(p[0],A)>dis(p[0],B))        return 1;    return -1;}double graham(int n) {    int i,top,k;    double sum;    temp=p[0];    for(i=1;i<n;i++){//找到下左位置的点         if(p[i].y<temp.y||(p[i].y==temp.y&&p[i].x<temp.x)){            temp=p[i];            k=i;        }    }    if(k!=0){        temp=p[0];        p[0]=p[k];        p[k]=temp;    }    qsort(p+1,n-1,sizeof(p[0]),cmp);//排序     res[0]=p[0];    res[1]=p[1];    for(i=top=2;i<n;i++){        while(top>1&&multi(res[top-2],res[top-1],p[i])<=0)            top--;        res[top++]=p[i];    }    for(i=0,sum=0;i<top-1;i++){        //printf("%lf %lf\n",res[i].x,res[i].y);        sum+=dis(res[i],res[(i+1)%top]);    }    sum+=dis(res[top-1],res[0]);    return sum;}int main() {    int T,i,n,count=0;    double ans,l;    scanf("%d",&T);    while(T--){        if(count)            printf("\n");        count=1;        scanf("%d %lf",&n,&l);        for(i=0;i<n;i++){            scanf("%lf %lf",&p[i].x,&p[i].y);        }        ans=graham(n);        ans+=PI*(l+l);        printf("%.0lf\n",ans);    }    return 0;}/*49 100200 400300 400300 300400 300400 400500 400500 200350 200200 200*/
 
代码二:
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#define N 1100#define PI 3.1415927struct point{    int x,y;    double dis;}p[N],res[N],temp;int cmp2(const void *a, const void *b){    struct point *A,*B;    A=(struct point *)a;    B=(struct point *)b;    if(A->x!=B->x)        return A->x-B->x;    else        return A->y-B->y;}double getdis(struct point a, struct point b){    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}void find(int n){    int i,k=0;    temp=p[0];    for(i=1;i<n;i++){        if(p[i].y<temp.y||(p[i].y==temp.y&&p[i].x<temp.x)){            temp=p[i];            k=i;        }    }    if(k!=0){        temp=p[0];        p[0]=p[k];        p[k]=temp;    }    for(i=1,p[0].dis=0;i<n;i++){        p[i].dis=getdis(p[0],p[i]);    }}int cmp(const void *a, const void *b) {    struct point *A,*B;    A=(struct point *)a;    B=(struct point *)b;    if((A->y-p[0].y)*(B->x-p[0].x)!=(A->x-p[0].x)*(B->y-p[0].y))        return atan2(A->y-p[0].y,A->x-p[0].x)>atan2(B->y-p[0].y,B->x-p[0].x)?1:-1;    else        return A->dis>B->dis?1:-1;}double graham(int n) {    int i,top;    double sum;    res[0]=p[0];    res[1]=p[1];    for(i=top=2;i<n;i++){        while(top>1&&check(res[top-2],res[top-1],p[i])<=0)            top--;        res[top++]=p[i];    }    for(i=0,sum=0;i<top;i++){        printf("%d %d\n",res[i].x,res[i].y);        sum+=getdis(res[i],res[(i+1)%top]);    }    return sum;}int main() {    int T,n,i,l;    double ans;    //freopen("in.txt","r",stdin);    //freopen("out2.txt","w",stdout);    scanf("%d",&T);    while(T--){        scanf("%d %d",&n,&l);        for(i=0;i<n;i++){            scanf("%d %d",&p[i].x,&p[i].y);        }        find(n);        qsort(p+1,n-1,sizeof(p[0]),cmp);        ans=graham(n);        ans+=PI*(l+l);        printf("%.0lf\n",ans);        if(T)            printf("\n");    }    return 0;}/*9 100200 400300 400300 300400 300400 400500 400500 200350 200200 200*/
 
本人随机生产了1000组测试数据,结果和别人AC的代码完全一样,但是程序一直WA,各种无奈,哪位大神看到之后,希望不吝赐教!
原创粉丝点击