HDOJ1392凸包模版

题目:题目链接

Problem Description
There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you help him?
The diameter and length of the trees are omitted, which means a tree can be seen as a point. The thickness of the rope is also omitted which means a rope can be seen as a line.



There are no more than 100 trees.
 
Input
The input contains one or more data sets. At first line of each input data set is number of trees in this data set, it is followed by series of coordinates of the trees. Each coordinate is a positive integer pair, and each integer is less than 32767. Each pair is separated by blank.

Zero at line for number of trees terminates the input for your program.
 
Output
The minimal length of the rope. The precision should be 10^-2.
 
Sample Input
  
  
9 12 7 24 9 30 5 41 9 80 7 50 87 22 9 45 1 50 7 0
 
Sample Output
  
  
243.06
这道题就是套模版,最后直接求距离就可以了,上代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct node
{
    int x,y;
};
node vex[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,node,node);
double dis(node,node);
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;
}
node stackk[1000];
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));
}
int main()
{
    int t;
    while(scanf("%d",&t),t!=0)
    {
        int i;
        for(i=0; i<t; i++)
        {
            scanf("%d%d",&vex[i].x,&vex[i].y);
        }
        if(t==1)
            printf("%.2f\n",0.00);
        else if(t==2)
            printf("%.2f\n",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]);
            printf("%.2f\n",s);
        }
    }
    return 0;
}

上一题的HDOJ的1348的代码也可以改改就用,试试...

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

const int N = 105;
const double eps = 1e-8;

struct point {
    int x;
    int y;
}p[N], stack[N];

bool isZero(double x) {
    return (x > 0 ? x : -x) < eps;
}

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 crossProd(point A, point B, point C) {
    return (B.x-A.x)*(C.y-A.y) - (B.y-A.y)*(C.x-A.x);
}

//以最左下的点为基准点,其他各点(逆时针方向)以极角从小到大的排序规则
int cmp(const void *a, const void *b) {
    point *c = (point *)a;
    point *d = (point *)b;
    double k = crossProd(p[0], *c, *d);//极角大小转化为求叉乘
    if (k<eps || isZero(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 mi = 0;
    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;
            mi = i;
        }
    }
    point tmp = p[mi];
    p[mi] = p[0];
    p[0] = tmp;
    qsort(p+1, n-1, sizeof(point), cmp);
    p[n] = p[0];
    stack[0] = p[0];
    stack[1] = p[1];
    stack[2] = p[2];
    int top = 2;
    for (int i=3; i<=n; ++i) {//加入一个点后,向右偏拐或共线,则上一个点不在凸包内,则--top,该过程直到不向右偏拐或没有三点共线的点 
        while (crossProd(stack[top-1], stack[top], p[i])<=eps && top>=2) --top;
        stack[++top] = p[i];//在当前情况下符合凸包的点,入栈
    }
    double len = 0;
    for (int i=0; i<top; ++i) len += dis(stack[i], stack[i+1]);
    return len;
}

int main() {
    int n;
    while (scanf("%d", &n), n) {
        for (int i=0; i<n; ++i) scanf ("%d%d", &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 = Graham(n);
            printf ("%.2lf\n", len);
        }
    }
    return 0;
}

努力努力...

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值