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. 关于VS2008中的targetver.h文件

    targerver.h文件的作用: 定义程序运行的环境,如限制程序只能在XP下运行,限制程序在只能在Vin7下运行 或限制程序只能在XP以上系统运行,或限制程序只能在Server2003以上系统运行. ...

  2. jquery模仿css3延迟效果

    HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...

  3. Android布局自定义Shap圆形ImageView,可以单独设置背景与图片

    一.图片预览:                  一.实现功能: 需求要实现布局中为圆形图片,图片背景与图标分开且合并到一个ImageView. 二.具体实现: XML中布局中定义ImageView, ...

  4. CSS 设计彻底研究(三)深入理解盒子模型

    第三章 深入理解盒子模型 盒子模型是CSS控制页面的基础.需要清楚“盒子”的含义是什么,以及盒子的组成.此外,应该理解DOM的基本概念,以及DOM树是如何与一个HTML文档对应的,在此基础上充分理解“ ...

  5. IT学习方法

    IT 技术的发展日新月异,新技术层出不穷,具有良好的学习能力,能及时获取新知识.随时补充和丰富自己,已成为程序员职业发展的核心竞争力.本文中,作者结合多年的学习经验总结出了提高程序员学习能力的三个要点 ...

  6. js倒计时 重发 效果

    <script type="text/javascript"> window.onload = function() { var wait = 60; function ...

  7. [Head First Python]6. summary

    1- 字典-内置数据结构,数据值与键值关联 键-字典中查找部分 值-字典中数据部分 使用dict()工厂函数或者只用{}可以创建一个空字典 >>> list = {} >> ...

  8. project euler 25 fibonacci

    数学方法: Saying that a number contains 1000 digits is the same as saying that it's greater than 10**999 ...

  9. MySQL - 建库、建表、查询

    本章通过演示如何使用mysql客户程序创造和使用一个简单的数据库,提供一个MySQL的入门教程.mysql(有时称为“终端监视器”或只是“监视”)是一个交互式程序,允许你连接一个MySQL服务器,运行 ...

  10. 前端MVVM学习之KnockOut(二)

    现在开始学习Knockout并且做个简单的例子. Knockout是建立在以下三个核心功能之上的: 1.Observables and dependency tracking(属性监控与依赖跟踪) 2 ...