【计算几何初步-凸包-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 ...
随机推荐
- Android窗口管理服务WindowManagerService对壁纸窗口(Wallpaper Window)的管理分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8550820 Android系统中,壁纸窗口和输 ...
- 又一道软通动力7K月薪面试题——银行业务调度系统
后期补充:网友对我诟病最多的就是我帮学生做面试题,说这是小偷和骗子行为,在此,我对自己给学员做面试题做出例如以下解释: (1)学员拿着面试题来找老师,学生也事先思考和尝试后实在没有办法,又求职心切才 ...
- [Firmware Warn]: GHES: Failed to read error status block address for hardware error source
Firmware Warn 问题描述: 系统版本:Ubuntu 12.04 LTS. 系统启动后dmesg打印大量Firmware Warn告警信息到syslog文件中.信息如下: [Firmware ...
- Js异步级联选择框实践方法
HTML: <li> <span>所在地区:</span> <select name="prov" id="ddl_prov&q ...
- js控制html5 audio的暂停、播放、停止
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <meta name ...
- 转化json
/// <summary> /// 转换成JSON字符串 /// </summary> /// <param name="jsonObject"> ...
- iBatis2之SqlMap配置总结(18条)
iBatis2之SqlMap配置总结(18条) SqlMap的配置是iBatis中应用的核心.这部分任务占据了iBatis开发的70的工作量. 1.命名空间: <sqlMap names ...
- SSH连接LINUX乱码解决方法
1.vi /etc/sysconfig/i18n 将内容改为 LANG="zh_CN.GB18030" LANGUAGE="zh_CN.GB18030:zh_CN.GB2 ...
- BlockingQueue
BlockingQueue的使用 http://www.cnblogs.com/liuling/p/2013-8-20-01.html BlockingQueue深入分析 http://blog.cs ...
- (原创) mac 10.9.2 eclipse 的 CDT 的 异常的修复
测试平台:macbook air 2012 , os x 10.9.2 , eclipse 4.3 在升级了 10.9 之后,eclipse 的CDT 无法正常使用了 异常表现: 1. 文 ...