hdu1392 Surround the Trees 凸包
第一次做凸包,这道题要特殊考虑下,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 凸包的更多相关文章
- HDU-1392 Surround the Trees,凸包入门!
Surround the Trees 此题讨论区里大喊有坑,原谅我没有仔细读题还跳过了坑点. 题意:平面上有n棵树,选一些树用绳子围成一个包围圈,使得所有的树都在这个圈内. 思路:简单凸包入门题,凸包 ...
- hdu 1392 Surround the Trees 凸包模板
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1392 Surround the Trees (凸包)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU - 1392 Surround the Trees (凸包)
Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出 ...
- hdu 1392 Surround the Trees 凸包裸题
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDUJ 1392 Surround the Trees 凸包
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 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 ...
- 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 ...
- HDU 1392 Surround the Trees(凸包*计算几何)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 这里介绍一种求凸包的算法:Graham.(相对于其它人的解释可能会有一些出入,但大体都属于这个算 ...
随机推荐
- 问题在哪?动态菜单条-------Day86
今天做了一个动态菜单条,先上图片,简单说一下我想实现的效果: 就是以下这个地方,随着鼠标指到哪,它就划到哪,并有一个惯性的幅度,并且滑动距离越远,停住的时候惯性越大,摆动幅度越大,这就是我大概想实现的 ...
- WinForm 使用皮肤,且单击按更换皮肤。
运行效果: 首先把DLL程序集文件和SSK皮肤文件放在要运行程序的DEBug文件夹下,然后引入引用. 之后可以在程序里写代码了. private void Form2_Load(object send ...
- RadioButtonList控件
在这里只写,绑定数据库数据的RadioButtonList控件: 一: 首先,先在数据库中建立一张表: 1 CREATE TABLE KK 2 ( 3 id INT, 4 [name] VARCHAR ...
- 离散傅立叶变换与快速傅立叶变换(DFT与FFT)
自从去年下半年接触三维重构以来,听得最多的词就是傅立叶变换,后来了解到这个变换在图像处理里面也是重点中的重点. 本身自己基于高数知识的理解是傅立叶变换是将一个函数变为一堆正余弦函数的和的变换.而图像处 ...
- WPF Multi-Touch 开发:基础触屏操作(Raw Touch)
原文 WPF Multi-Touch 开发:基础触屏操作(Raw Touch) 多点触控(Multi-Touch)就是通过与触屏设备的接触达到人与应用程序交互的操作过程.例如,生活中经常使用的触屏手机 ...
- Android 它们的定义View它BounceProgressBar
转载请注明出处:http://blog.csdn.net/bbld_/article/details/41246247 [Rocko's blog] 之前几天下载了非常久没用了的桌面版酷狗来用用的时候 ...
- UITabBarController中自定义UITabBar
1.创建多个视图控制器,放如UITabBarController中 AViewController *aa = [[AViewController alloc] init]; UINavigation ...
- cocos2dx-lua捕获用户touch事件的几种方式
这里仅仅针对lua 1.为每一个关心的事件注冊回调函数 详细分为下面几种 1>单点触摸 注冊函数为 cc.Handler.EVENT_TOUCH_BEGAN = 40 cc.Handl ...
- jQuery 查询 xml
加载xml:(注:这个方法在Chrome是用不了的,Chrome是禁止访问本地的xml文件,在ie11里面用不了,ie11的xmlDom文档没有load方法) function loadXML(fil ...
- uboot代码2:stage2代码,启动内核
一.uboot最终目的: 1.读出内核 do_nand read kernel { flash上存的内核:uImage = 头部 + 真正的内核; } 2.启动内核. do_bootm_linux { ...