HDU1348-Walls(经典凸包)

探讨如何通过计算几何中的格拉姆扫描算法解决国王提出的围绕城堡建设围墙的问题,确保围墙距离城堡一定距离的同时使用最少资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
1 9 100 200 400 300 400 300 300 400 300 400 400 500 400 500 200 350 200 200 200
 

Sample Output
1628
 
题目大意:任意给你一个多边形,要求在多边形的外围建一堵围墙,要求是用的石头最少,并且多边形离墙体的距离不能大于给定的L,求外围墙的最少长度。
分析:最近学习凸包,刚开始用atan2排序,然后看了大牛的代码,不用atan2排序,直接就排了,写了两种代码,莫名的WA,求指点。。。
代码一:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define PI 3.1415927
struct 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;
}


/*

4
9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200
*/
 
代码二:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define N 1100
#define PI 3.1415927
struct 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 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200
*/
 
本人随机生产了1000组测试数据,结果和别人AC的代码完全一样,但是程序一直WA,各种无奈,哪位大神看到之后,希望不吝赐教!

内容概要:本文档详细介绍了基于MATLAB实现多目标差分进化(MODE)算法进行无人机三维路径规划的项目实例。项目旨在提升无人机在复杂三维环境中路径规划的精度、实时性、多目标协调处理能力、障碍物避让能力和路径平滑性。通过引入多目标差分进化算法,项目解决了传统路径规划算法在动态环境和多目标优化中的不足,实现了路径长度、飞行安全距离、能耗等多个目标的协调优化。文档涵盖了环境建模、路径编码、多目标优化策略、障碍物检测与避让、路径平滑处理等关键技术模块,并提供了部分MATLAB代码示例。 适合人群:具备一定编程基础,对无人机路径规划和多目标优化算法感兴趣的科研人员、工程师和研究生。 使用场景及目标:①适用于无人机在军事侦察、环境监测、灾害救援、物流运输、城市管理等领域的三维路径规划;②通过多目标差分进化算法,优化路径长度、飞行安全距离、能耗等多目标,提升无人机任务执行效率和安全性;③解决动态环境变化、实时路径调整和复杂障碍物避让等问题。 其他说明:项目采用模块化设计,便于集成不同的优化目标和动态环境因素,支持后续算法升级与功能扩展。通过系统实现和仿真实验验证,项目不仅提升了理论研究的实用价值,还为无人机智能自主飞行提供了技术基础。文档提供了详细的代码示例,有助于读者深入理解和实践该项目。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值