【计算几何初步-凸包-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 ...
随机推荐
- Linux 终端訪问 FTP 及 上传下载 文件
今天同事问我一个问题,在Linux 下訪问FTP,并将文件上传上去. 我之前一直是用WinSCP工具的. 先将文件从linux copy到windows下,然后在传到ftp上. google 一下. ...
- Udacity调试课笔记之断言异常
Udacity调试课笔记之断言异常 这一单元的内容不是很多,如Zeller教授所说,就是如何写.检查断言,并如何使用工具实现自动推导出断言的条件. 现在,多数的编程语言,尤其是高级编程语言都会有内置的 ...
- php字符串标点等字符截取不乱吗 封装方法
方法一: /** +---------------------------------------------------------- * 功能:字符串截取指定长度 * leo.li hen ...
- WEB服务器6--IIS架构补充篇
第一部分我将谈谈IIS的两个不同的版本—IIS 5.x 和 IIS 6的处理模型:IIS如何监听来自外界的Http request,如何根据ISAPI Extension Mapping将对于不同Re ...
- iOS-OC-基础-NSNumber常用方法
/*===========================NSNumber数值对象=========================*/ // 将基本数据类型保存为NSNumber 对象类型 NSNu ...
- struts2中的action访问web对象
Struts2的Action就是一个普通的POJO对象,它和Web对象request.response.session和application没有耦合在一起,这样便于单独测试Action,那么我们在A ...
- java连接sqL2008 数据库实例
package com.lzw; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSe ...
- (转)Java程序利用main函数中args参数实现参数的传递
Java程序利用main函数中args参数实现参数的传递 1.运行Java程序的同时,可以通过输入参数给main函数中的接收参数数组args[],供程序内部使用!即当你在Java命令行后面带上参数,J ...
- C语言实现界面(不通过MFC\避免遗忘)
感觉MFC不属于程序员细究的东西,今实现基本界面避免日后遗忘. 源代码: #include<windows.h>#include<stdio.h>char str[] = {' ...
- 数值的N次方
问题描述: 实现函数double Power(double base,int exponent),求base的exponent次方.不得使用库函数, 同时不需考虑大数问题. 思路分析: 要是你秒秒钟想 ...