第一次做凸包,这道题要特殊考虑下,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. ViewPager控件的Demo

    1.主视图 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...

  2. svn: keywords

    在文件头里面加入下面的关键字: $Date$ $ID$ $Revision$ $Author$ 代码在svn提交时,先选中这几个关键字再提交. Date可能出现中文乱码: 在Control Panel ...

  3. sqlplus

    以超级管理员登录 sqlplus sys/123 as sysdba 解锁用户 alter user xutianhao account unlock

  4. vim打开文件时显示行号

    vim打开文件是,默认不提示行号. 至于显示行号的用途,因人而异 linux下一个主机可能有N个账户.对于配置分为两种:仅配置当前账户,配置所有账户 vim配置文件路径(Centos  5.5 fin ...

  5. Home键的获取监听,安卓4.0后就不能在onkeydown方法中获取了。怎么办。

    Android下得到Home键按下的消息   在Android下,并不能通过onKeyDown这样的事件来截获Home键的消息,其原因在Android的文档中已经明确的说过了 public stati ...

  6. jackson 转json. 过滤null值

    @Test public void tttttt() throws JsonGenerationException, JsonMappingException, IOException { Objec ...

  7. HNCU1330:算法3-1:八进制数

    http://hncu.acmclub.com/index.php?app=problem_title&id=111&problem_id=1330 题目描述 将十进制数转换为八进制, ...

  8. Android应用公布的准备——渠道注冊与认证

    今天早上申请了一个早上的渠道账号,这工作真是太繁琐,申请的是企业账号,须要营业执照等相关资料,假设申请个人的话预计须要身份证相关信息.以下贴出国内主流的几个渠道.不全然,可是基本上涵盖了大部分. 36 ...

  9. 基于visual Studio2013解决面试题之0709求方

     题目

  10. cocos2d-x游戏开发系列教程-坦克大战游戏关卡选择场景的编写下

    上篇文章写了Paddle类来处理精灵的点击.触摸事件,现在我们在Paddle的基础上 写一个MyPaddle类,来处理上一关.下一关.开始游戏按钮的点击事件. 1.类声明如下: class MyPad ...