Surround the Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8113    Accepted Submission(s): 3095

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
9
12 7
24 9
30 5
41 9
80 7
50 87
22 9
45 1
50 7
0
 
Sample Output
243.06
 
Source
 
Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:  2150 1348 1147 1558 1374 

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的更多相关文章

  1. HDU-1392 Surround the Trees,凸包入门!

    Surround the Trees 此题讨论区里大喊有坑,原谅我没有仔细读题还跳过了坑点. 题意:平面上有n棵树,选一些树用绳子围成一个包围圈,使得所有的树都在这个圈内. 思路:简单凸包入门题,凸包 ...

  2. 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 ...

  3. 【计算几何初步-凸包-Graham扫描法-极角序】【HDU1348】 WALL

    Wall Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  4. hdu1392 Surround the Trees 凸包

    第一次做凸包,这道题要特殊考虑下,n=2时的情况,要除以二才行. 我是从最左边的点出发,每次取斜率最大的点,一直到最右边的点. 然后从最左边的点出发,每次取斜率最低的点,一直到最右边的点. #incl ...

  5. [HDU1392]Surround the Trees

    思路: 凸包模板题. 注意n=1和n=2的情况. 当n=1时,不需要绳子. 当n=2时,绳子长度为两棵树之间距离. 当n≥e时,Graham求凸包即可.最后将凸包上的所有相邻点距离求和. #inclu ...

  6. 计算几何 : 凸包学习笔记 --- Graham 扫描法

    凸包 (只针对二维平面内的凸包) 一.定义 简单的说,在一个二维平面内有n个点的集合S,现在要你选择一个点集C,C中的点构成一个凸多边形G,使得S集合的所有点要么在G内,要么在G上,并且保证这个凸多边 ...

  7. hdu 1392:Surround the Trees(计算几何,求凸包周长)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. HDU1392:Surround the Trees(凸包问题)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. HDU 1392 Surround the Trees(凸包入门)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. VC调试笔记

    1.windows-32调试: ①使用map文件根据崩溃地址寻找对应的源代码文件和行号 勾选project->settings->link->General mapfile,对应的P ...

  2. 连载:面向对象葵花宝典:思想、技巧与实践(33) - ISP原则

     ISP,Interface Segregation Principle,中文翻译为"接口隔离原则". 和DIP原则一样,ISP原则也是大名鼎鼎的Martin大师提出来的,他在19 ...

  3. 利用PHP/MYSQL实现的简易微型博客(转)

    数据库:ly_php_base 表:ly_micro_blog(仅仅有一个表)字段:id,title,date,content,hits 文件: 文件 描述 default.php 默认主页.显示博文 ...

  4. 对Lucene PhraseQuery的slop的理解[转载]

    所谓PhraseQuery,就是通过短语来检索,比如我想查“big car”这个短语,那么如果待匹配的document的指定项里包含了"big car"这个短语,这个documen ...

  5. Emacs下编译C++/C程序<转>

    1.启动Emacs,在终端输入“emacs&”命令后回车(你也可以输入“emacs”命令,不过当你在使用Emacs的时候,当前终端 就不为你工作了:并且如果你熟练使用Emacs的话也可以输入“ ...

  6. HTML5简单入门系列(三)

    前言 本篇介绍HTML5支持的Web存储(Web Storage)和HTML 5 应用程序缓存. 客户端存储数据介绍 HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没 ...

  7. sitemap.xml 静态和动态生成页面 shopnc二次开发 动态生成sitemap.xml

    Sitemap 可方便网站管理员通知搜索引擎他们网站上有哪些可供抓取的网页.最简单的 Sitemap 形式,就是XML 文件,在其中列出网站中的网址以及关于每个网址的其他元数据(上次更新的时间.更改的 ...

  8. centos源码安装git

    因为Centos上yum安装的话可能版本比较低,使用中会有一些难以预料的问题出现. 从源代码编译安装方法: #Centos执行: yum install curl-devel expat-devel ...

  9. nginx之如何获取真实客户端ip

    nginx的配置文件中日志格式加入$http_x_forwarded_for--> log_format access '$remote_addr - $remote_user [$time_l ...

  10. project euler 16:Power digit sum

    >>> sum([int(i) for i in str(2**1000)]) 1366 >>>