HDU-#1392 Surround the Trees(Graham & Andrew +凸包)

      题目大意:给出树林各个树的坐标,求围住全部树木的最小周长是多少?

     解题思路:该题很明显是凸包问题,很简单的凸包模板题。这里给出了两种求凸包的算法,一个是Graham算法,二是Andrew 算法。后者相对于前者来说更快,数值稳定性更好。这只是最基础的两个版本,关于这两个算法还有很多改进版本,以后慢慢总结下。该题解法就是凸包的最基础应用,详见code。

     题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1392

     Graham code:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
using namespace std;

const double eps = 1e-10;
const int MAXN = 100+10;
int n;

struct point{
    double x,y;
}p[MAXN],st[MAXN];

int dcmp(double x){ //精度比较
    if(fabs(x)<eps) return 0;
    else return x<0 ? -1:1;
}

double Cross(point A,point B,point C){ //叉乘
    return (B.x-A.x)*(C.y-A.y) - (B.y-A.y)*(C.x-A.x);
}

double dis(point A, point B) { //距离公式
    return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}

int cmp(const void *a, const void *b) { //自定义比较函数:基准点为最左下点,逆时针方向,进行极角排序
    point *c = (point *)a;
    point *d = (point *)b;
    double k = Cross(p[0], *c, *d);
    if (k<eps || dcmp(k) && dis(p[0], *c)>dis(p[0], *d)) return 1;
    return -1;
}

double Graham(int n){
    int x=p[0].x;
    int y=p[0].y;
    int cnt=0;
    if(n==1) return 0; //边界值判断
    if(n==2) return dis(p[0],p[1]);
    for(int i=1;i<n;i++)//找到基准点
        if(p[i].x<x || (p[i].x==x && p[i].y<y)){
            x=p[i].x;
            y=p[i].y;
            cnt=i;
        }
    point tmp=p[cnt];
    p[cnt]=p[0];
    p[0]=tmp;
    qsort(p+1, n-1, sizeof(point), cmp); //极角排序
    p[n]=p[0];
    st[0]=p[0];
    st[1]=p[1];
    st[2]=p[2];
    int top=2;
    for(int i=3;i<=n;i++){ //凸包过程
        while(top>=2 && Cross(st[top-1],st[top],p[i])<=eps) --top;
        st[++top]=p[i];
    }
    double len=0; //计算周长
    for(int i=0;i<top;i++) len+=dis(st[i],st[i+1]);
    return len;
}

int main(){
    while(scanf("%d",&n)!=EOF && n){
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        double len=Graham(n);
        printf("%.2lf\n",len);
    }
    return 0;
}

     Andrew code:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

const double eps = 1e-10;
const int MAXN = 100+10;
int n;

struct point{
    double x,y;
    point(double x=0,double y=0):x(x),y(y){} //构造函数
};

point operator - (point A,point B){return point(A.x-B.x,A.y-B.y);} //重载向量减号
bool operator < (const point& a,const point& b){return a.x<b.x || (a.x==b.x && a.y<b.y);} //重载向量小于比较

double Cross(point A,point B){return A.x*B.y-A.y*B.x;} //叉积
double dis(point A, point B) {return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));} //距离公式

double Andrew(point* p,int n,point* ch){
    sort(p,p+n); //水平序:先比较x坐标,在比较y坐标
    int m=0; //记录顶点数
    for(int i=0;i<n;i++){ //凸包过程:下凸包
        while(m>1 && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]) <=eps) m--;
        ch[m++]=p[i];
    }
    int k=m;
    for(int i=n-2;i>=0;i--){ //凸包过程:上凸包
        while(m>k && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]) <=eps) m--;
        ch[m++]=p[i];
    }
    if(n>1) m--;
    double len=0; //计算周长
    for(int i=0;i<m;i++) len+=dis(ch[i],ch[i+1]);
    return len;
}

int main(){
    while(scanf("%d",&n)!=EOF && n){
        point p[MAXN],ch[MAXN];
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        if (n == 1) printf ("0.00\n"); //特殊点计算
        else if (n == 2) printf ("%.2lf\n", dis(p[0], p[1]));
        else {
            double len=Andrew(p,n,ch);
            printf ("%.2lf\n", len);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值