第一次做凸包,这道题要特殊考虑下,n=2时的情况,要除以二才行。

我是从最左边的点出发,每次取斜率最大的点,一直到最右边的点。

然后从最左边的点出发,每次取斜率最低的点,一直到最右边的点。

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
using namespace std;
const double eps=1e-9;
struct node
{
double x,y;
}f[105];
bool flag[105];
int n;
bool cmp(node a,node b)
{
return fabs(a.x-b.x)<eps?(a.y<b.y):(a.x<b.x);
}
double xielv(int a,int b)//求斜率
{
return (f[a].y-f[b].y)/(f[a].x-f[b].x);
}
double dis(int a,int b)//求距离
{
double xx=f[a].x-f[b].x,yy=f[a].y-f[b].y;
return sqrt(xx*xx+yy*yy);
}
void calans()//计算最短周长
{
int i,j,now=0,maxi;
double ans=0;
while(1)
{
double max=-4000000;//斜率最大值
maxi=now;
for(i=1;i<n;i++)
{
if(flag[i])continue;
if(-f[i].x+f[now].x>eps)continue;//在当前点的左边,不考虑
if(fabs(f[now].x-f[i].x)<eps)
{
if(f[now].y>f[i].y)//在当前点的正下方,不考虑
continue;
maxi=i;
break;
}
else
{
double p=xielv(now,i);
if(p>max)
{
maxi=i;
max=p;
}
}
}
flag[maxi]=1;
ans+=dis(now,maxi);
if(maxi==now)
{
flag[maxi]=0;//最后右边的点要计算两次,去除标记
break;
}
now=maxi;
}
now=0;
while(1)//每次取斜率最小的点
{
double min=4000000;
int mini=now;
for(i=1;i<n;i++)
{
if(flag[i])continue;
if(-f[i].x+f[now].x>eps)continue;//在当前点的左边,不考虑 if(fabs(f[i].x-f[now].x)<eps)
{
if(f[i].x+0.1<f[n-1].x)//在当前点的正下方,不考虑 continue;
mini=i;
break;
}
double p=xielv(now,i);
if(p<min)
{
mini=i;
min=p;
}
}
ans+=dis(now,mini);
flag[mini]=1;
if(mini==now)
break;
now=mini;
}
if(n==2)ans/=2;//n=2时,除以二
printf("%.2f\n",ans);
}
int main()
{
int i,j;
while(scanf("%d",&n)!=-1&&n)
{
memset(flag,0,sizeof(flag));
for(i=0;i<n;i++)
scanf("%lf%lf",&f[i].x,&f[i].y);
sort(f,f+n,cmp);
calans();
}
return 0;
}

hdu1392 Surround the Trees 凸包的更多相关文章

  1. HDU-1392 Surround the Trees,凸包入门!

    Surround the Trees 此题讨论区里大喊有坑,原谅我没有仔细读题还跳过了坑点. 题意:平面上有n棵树,选一些树用绳子围成一个包围圈,使得所有的树都在这个圈内. 思路:简单凸包入门题,凸包 ...

  2. hdu 1392 Surround the Trees 凸包模板

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. hdu 1392 Surround the Trees (凸包)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. HDU - 1392 Surround the Trees (凸包)

    Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出 ...

  5. hdu 1392 Surround the Trees 凸包裸题

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. HDUJ 1392 Surround the Trees 凸包

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. ACM学习历程—HDU1392 Surround the Trees(计算几何)

    Description There are a lot of trees in an area. A peasant wants to buy a rope to surround all these ...

  8. HDU 1392 Surround the Trees (凸包周长)

    题目链接:HDU 1392 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope ...

  9. HDU 1392 Surround the Trees(凸包*计算几何)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 这里介绍一种求凸包的算法:Graham.(相对于其它人的解释可能会有一些出入,但大体都属于这个算 ...

随机推荐

  1. eclipse java快捷模板 快捷键大全

    建议没事研究研究自己吃饭的工具,俗话说工欲善其事必先利其器嘛. 首先,快捷键这种东西大家都会知道点,但是很少人重视javaEditorTemplate这块.先介绍下Template java编辑模板 ...

  2. 【转】linux下 postgres的一些操作总结

    参考博文: PostgreSQL详解     1. 基本操作命令 安装完成后,PostgreSQL默认创建了名为postgres数据库用户账户,其与MySQL的root以及SQL Server的sa账 ...

  3. @(报错)could not find the main class, Program will exit(已解决)

    原文 @(报错)could not find the main class, Program will exit(已解决)      (很抱歉,如果你希望能更加清楚地看清图片或是图上的文字的话,你可以 ...

  4. TF-IDF与余弦相似性的应用(一):自动提取关键词 - 阮一峰的网络日志

    TF-IDF与余弦相似性的应用(一):自动提取关键词 - 阮一峰的网络日志     TF-IDF与余弦相似性的应用(一):自动提取关键词     作者: 阮一峰     日期: 2013年3月15日 ...

  5. Ext的异步请求(二级级联动态加载下拉列表)

    页面: <tr> <td class="label" width="300" >作业计划项模板</td> <td> ...

  6. 正则表达式引擎的构建——基于编译原理DFA(龙书第三章)——3 计算4个函数

    整个引擎代码在github上,地址为:https://github.com/sun2043430/RegularExpression_Engine.git nullable, firstpos, la ...

  7. OpenRisc-35-基于orpsoc,eCos的sd card controller的测试实验

    引言 之前,曾经在orpsoc的平台上,测试验证过其sd card controller的linux的驱动,但是并不是很完美,经过努力,终于在eCos下完成了其全部功能的验证,包括驱动层验证,文件系统 ...

  8. 在jsp页面下, 让eclipse完全支持HTML/JS/CSS智能提示(转)

      我们平时用eclipse开发jsp页面时智能提示效果不太理想,今天用了两个小时发现了eclipse也可以像Visual Studio 2008那样完全智能提示HTML/JS/CSS代码,使用ecl ...

  9. C#2.0--集合--转载车老师

    集合在编程的过程中用的是非常的多,如GridViewRowCollection.ConnectionStringSettingsCollection.NameValueCollection等等.一般来 ...

  10. 用Delphi实现Windows的鼠标钩子函数

    Delphi是基于PASCAL语言的Windows编程工具,功能十分强大.然而在Delphi的帮助文件中,对Windows API函数的说明沿袭了 VC 的格式,和VC一样,对很多API函数的用法没有 ...