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.(相对于其它人的解释可能会有一些出入,但大体都属于这个算 ...
随机推荐
- 为Cocos2d-x的Android平台加入Protobuffer支持
为Cocos2d-x的Android平台加入Protobuffer支持 分类: 工作2013-11-27 18:00 386人阅读 评论(1) 收藏 举报 cocos2d-xandroid平台交叉编译 ...
- InheritableThreadLocal
InheritableThreadLocal继承自ThreadLocal,但比ThreadLocal多一个特性: 子线程可以继承父亲线程上下文中的信息 但是,有两个点需要注意的: 只有子线程创建之前的 ...
- django-form表单的提交
<form action="/blog/" method="get"> 如果runserver默认启用127.0.0.1:8000的话,且上面这个f ...
- mysql各版本区别
MySQL 的官网下载地址:http://www.mysql.com/downloads/ 在这个下载界面会有几个版本的选择. 1. MySQL Community Server 社区版本,开源免费, ...
- HNCU1330:算法3-1:八进制数
http://hncu.acmclub.com/index.php?app=problem_title&id=111&problem_id=1330 题目描述 将十进制数转换为八进制, ...
- linux c setitimer使用方法说明
在linux c编程中.setitimer是一个比較经常使用的函数.可用来实现延时和定时的功能,网上有各种零零散散的使用方法说明,都仅仅提到了个别使用方法,今天抽出时间实践整理了一份比較具体的: 使用 ...
- fcntl()
fcntl() F_GETFL--------------------------------------------- 将文件状态标志作为函数值返回. 文件状态标志: ...
- 关于 调用 JNI JAR 的说明和注意事项,调用第三方 JAR SDK 和 翻译 安卓 JAVA 代码 的说明 V2015.6.10
关于 调用 JNI JAR 的说明和注意事项,调用第三方 JAR SDK 和 翻译 安卓 JAVA 代码 的说明 V2015.6.10 转载请标明出处,否则死全家.选择[复制链接]即可得到出处. (* ...
- Java读写Word文件常用技术
Java操作操作Word文件,最近花了几天时间解决使用Word模板导出数据的问题,收集到一些资料分享下. 常见的技术如下: 1.POI(兼容doc.docx文件) 官方网站:http://poi. ...
- 3DShader之立方体环境映射(cubic environment mapping)
前面讲了球形环境映射,然而目前采用更多的是立方体环境映射.国际惯例:上图先: 1.反射: 2.折射 3.fresnel(反射+折射) 4.色散 好了,大概讲下原理, 立方体纹理我就不多讲了,它以一个3 ...