【计算几何初步-凸包-Jarvis步进法。】【HDU1392】Surround the Trees
Surround the TreesTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Problem Description
There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you help him?
The diameter and length of the trees are omitted, which means a tree can be seen as a point. The thickness of the rope is also omitted which means a rope can be seen as a line. There are no more than 100 trees.
Input
The input contains one or more data sets. At first line of each input data set is number of trees in this data set, it is followed by series of coordinates of the trees. Each coordinate is a positive integer pair, and each integer is less than 32767. Each pair
is separated by blank. Zero at line for number of trees terminates the input for your program.
Output
The minimal length of the rope. The precision should be 10^-2.
Sample Input
Sample Output
Source
Recommend
|
http://www.cnblogs.com/Booble/archive/2011/02/28/1967179.html 可参考
Jarvis步进法:
算法流程
1.找横坐标最小的点(有一样的话纵坐标更小的)
2.从这点开始卷包裹 找最靠近外侧的点(通过叉积比较)
(注意一开始设k为0 若k=0随意找个点为其值)
3.找到的点为起点退出
注:为什么第二步的时候不怕找到过延长线的点?因为不可能存在这个点,若有这个点 在上一个点就被选择拓展了
叉积为0时 注意用点积判断 选择最外面那个点
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
#define exp 10e-6
using namespace std;
struct point{
double x,y;
};
double ans;
int start;
point a[101];
int visit[101];
int N;
double crossdet(double x1,double y1,double x2,double y2)
{
return x1*y2-x2*y1;
}
double cross(point A,point B,point C)
{
return crossdet(B.x-A.x,B.y-A.y,C.x-A.x,C.y-A.y); //AB*AC
}
double dotdet(double x1,double y1,double x2,double y2)
{
return x1*x2+y1*y2;
}
double dot(point A,point B,point C)
{
return dotdet(B.x-A.x,B.y-A.y,C.x-A.x,C.y-A.y); //AB dot AC
}
double dist(point A,point B)
{
return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}
int sgn(double A)
{
if(fabs(A)<exp) return 0;
else if(A>0) return 1;
else return -1;
}
void input()
{
ans=0;int sx=40000;int sy=40000;
memset(a,0,sizeof(a));
memset(visit,0,sizeof(visit));
for(int i=1;i<=N;i++)
{
scanf("%lf%lf",&a[i].x,&a[i].y);
if(sgn(a[i].x-sx)==-1)
{
sx=a[i].x;sy=a[i].y;
start=i;
}
else if(sgn(a[i].x-sx)==0&&sgn(a[i].y-sy)==-1)
{
sx=a[i].x;sy=a[i].y;
start=i;
}
}
}
void init()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
}
int FIND(int now)
{
int k=0;
for(int i=1;i<=N;i++)
{
if(visit[i]==0&&i!=now)
{
if(k==0)
{
k=i;
}
else
{
int temp=cross(a[now],a[k],a[i]);
if(sgn(temp)<0)
k=i;
if(sgn(temp)==0)
{
int temp2=dot(a[k],a[now],a[i]);
if(sgn(temp2)<0)
k=i;
}
}
}
}
visit[k]=1;
return k;
}
void solve()
{
int now=start,next;
while(1)
{
next=FIND(now);
ans+=dist(a[now],a[next]);
now=next;
if(next==start) break;
}
}
int main()
{
// init();
while(cin>>N&&N)
{
input();
solve();
if(N==2)
{
ans=dist(a[1],a[2]);
}
if(N==1) ans=0;
printf("%.2lf\n",ans);
}
return 0;
}
【计算几何初步-凸包-Jarvis步进法。】【HDU1392】Surround the Trees的更多相关文章
- HDU-1392 Surround the Trees,凸包入门!
Surround the Trees 此题讨论区里大喊有坑,原谅我没有仔细读题还跳过了坑点. 题意:平面上有n棵树,选一些树用绳子围成一个包围圈,使得所有的树都在这个圈内. 思路:简单凸包入门题,凸包 ...
- 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 ...
- 【计算几何初步-凸包-Graham扫描法-极角序】【HDU1348】 WALL
Wall Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- hdu1392 Surround the Trees 凸包
第一次做凸包,这道题要特殊考虑下,n=2时的情况,要除以二才行. 我是从最左边的点出发,每次取斜率最大的点,一直到最右边的点. 然后从最左边的点出发,每次取斜率最低的点,一直到最右边的点. #incl ...
- [HDU1392]Surround the Trees
思路: 凸包模板题. 注意n=1和n=2的情况. 当n=1时,不需要绳子. 当n=2时,绳子长度为两棵树之间距离. 当n≥e时,Graham求凸包即可.最后将凸包上的所有相邻点距离求和. #inclu ...
- 计算几何 : 凸包学习笔记 --- Graham 扫描法
凸包 (只针对二维平面内的凸包) 一.定义 简单的说,在一个二维平面内有n个点的集合S,现在要你选择一个点集C,C中的点构成一个凸多边形G,使得S集合的所有点要么在G内,要么在G上,并且保证这个凸多边 ...
- hdu 1392:Surround the Trees(计算几何,求凸包周长)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU1392: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 ...
随机推荐
- ORA-00119: invalid specification for system parameter LOCAL_LISTENER
重启oracle是提示错误ORA-00119: invalid specification for system parameter LOCAL_LISTENER. 解决方法: 命令查看错误信息:oe ...
- HTML - Textarea - 空格的问题解决方式
第一种方式: <textarea name="textareaname" rows="XX" cols="XX" ></t ...
- Javascript - IE8下parseInt()方法的取值异常
公司的测试小妹妹跑来对我说,下拉框第9项始终无法正确提交的时候,我还以为见鬼了. parseInt()会把'0'开头的数字以8进制来解析,当有大于7的数字时候就按10进制来解析. // p ...
- Android ---------- 清单文件中Activity常规设置
<activity android:name="xxxxx" android:alwaysRetainTaskState="true" android:c ...
- 关于UIWebview的属性的介绍
/* ViewController.h 文件 */ #import <UIKit/UIKit.h> @interface ViewController : ...
- 写一个Windows上的守护进程(4)日志其余
写一个Windows上的守护进程(4)日志其余 这次把和日志相关的其他东西一并说了. 一.vaformat C++日志接口通常有两种形式:流输入形式,printf形式. 我采用printf形式,因为流 ...
- [music]<蔷薇>
我在虾米音乐听到一首好听的歌曲<蔷薇>,一起来听吧!http://www.xiami.com/song/386109?ref=aother LOFTER:我们的故事 http://us ...
- zepto.1.1.6.js源码中的each方法学习笔记
each方法接受要遍历的对象和对应的回调函数作为参数,它的作用是: 1.如果要遍历的对象是类似数组的形式(以该对象的length属性值的类型是否为number类型来判断),那么就把以要遍历的对象为执行 ...
- Python爬虫学习:四、headers和data的获取
之前在学习爬虫时,偶尔会遇到一些问题是有些网站需要登录后才能爬取内容,有的网站会识别是否是由浏览器发出的请求. 一.headers的获取 就以博客园的首页为例:http://www.cnblogs.c ...
- php __FILE__,__CLASS__等魔术变量,及实例
今天突然看到几个自己不认识的魔术变量 不知道怎么用于是就上网查了一下,看到了这篇博客,写的真不错,希望自己以后也能学会这样总结 张映 发表于 2010-12-13 分类目录: php 标签:php, ...