*HDU 1392 计算几何
Surround the Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10403 Accepted Submission(s): 4033
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 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.
24 9
30 5
41 9
80 7
50 87
22 9
45 1
50 7
0
//求凸包的模板题Graham扫描法。
//详解《算法导论》604页
//极角排序先比较象限再比较叉积。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int INF=0x7fffffff;
int top,n,q[];//q用于保存组成凸包的点
struct Node { double x,y; }node[];
double dis(Node p1,Node p2)
{
return sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}
int Qd(Node p)//返回点相对于p[0]点所在的象限
{
p.x-=node[].x;
p.y-=node[].y;
if(p.x>=&&p.y>=) return ;
else if(p.x<=&&p.y>) return ;
else if(p.x<&&p.y<=) return ;
else if(p.x>=&&p.y<) return ;
}
double chaji(Node p0,Node p1,Node p2)//叉积
{
return ((p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x));
}
bool cmp(Node p1,Node p2)
{
int Q1=Qd(p1),Q2=Qd(p2);
if(Q1==Q2){
double tmp=chaji(node[],p1,p2);
if(tmp>) return ;//tem>0说明向量p1p0在向量p2p0的顺时针方向即p1p0相对于p0的极角小于p2p0的
else if(tmp<) return ;
else return fabs(p1.x)<fabs(p2.x);
}
else return Q1<Q2;
}
void tubao()
{
top=;
q[++top]=;
q[++top]=;
for(int i=;i<=n;i++){
while(top>&&chaji(node[q[top-]],node[q[top]],node[i])<=)
top--;
q[++top]=i;
}
}
int main()
{
while(scanf("%d",&n)&&n){
double min_x=INF,min_y=INF;
int min_i=;
for(int i=;i<n;i++){
scanf("%lf%lf",&node[i].x,&node[i].y);
if(min_y>node[i].y){
min_y=node[i].y;
min_x=node[i].x;
min_i=i;
}else if(min_y==node[i].y&&min_x<node[i].x){
min_x=node[i].x;
min_i=i;
}
}
swap(node[min_i],node[]);
if(n==) {printf("0.00\n");continue;}
if(n==) {printf("%.2lf\n",dis(node[],node[]));continue;}//计算凸包的点数必须多于2
sort(node+,node+n,cmp);
node[n].x=node[].x;node[n].y=node[].y;//形成闭合的凸包
tubao();
double ans=;
for(int i=;i<top;i++){
ans+=dis(node[q[i]],node[q[i+]]);
}
printf("%.2lf\n",ans);
}
return ;
}
*HDU 1392 计算几何的更多相关文章
- HDU 1392 凸包模板题,求凸包周长
1.HDU 1392 Surround the Trees 2.题意:就是求凸包周长 3.总结:第一次做计算几何,没办法,还是看了大牛的博客 #include<iostream> #inc ...
- HDU 1392 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:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出 ...
- HDU 1392 Surround the Trees (凸包周长)
题目链接:HDU 1392 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope ...
- HDU 1392 Surround the Trees(凸包*计算几何)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 这里介绍一种求凸包的算法:Graham.(相对于其它人的解释可能会有一些出入,但大体都属于这个算 ...
- hdu 1392: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
There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So a ...
- hdu 2108:Shape of HDU(计算几何,判断多边形是否是凸多边形,水题)
Shape of HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 1392 Surround the Trees
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意:给出一些点的坐标,求最小的凸多边形把所有点包围时此多边形的周长. 解法:凸包ConvexH ...
随机推荐
- set和map的简单用法
.set(集合)map(映射)都属于关联类容器 都支持查询一个元素是否存在并能够有效地获取元素. set集合的元素总是从小到大排列,set集合通过二分查找树实现.它具备以下两个特点: ①:独一无二的元 ...
- C# 反射
//转载:http://blog.csdn.net/educast/article/details/2894892 反射(Reflection) 在.NET中的反射可以实现从对象的外部来了解对象(或程 ...
- Ajax文件上传
- .NET 程序集Assembly使用
概述 一直以来,我们都在用C#编写程序,编写程序的时候,我们用到继承.多态.接口以及泛型,我们也都明白子类可以继承抽象类,并能够重写父类的抽象方法,可是大家是否想过,如下几个问题: 1.凡树必有根和叶 ...
- css sprite,css雪碧图生成工具V3.0更新
V3.0主要改进 1.增加了单独添加单张图片以及删除单张图片的功能 2.增加了生成.sprite文件用以保存雪碧图信息 3.增加了打开.sprite文件功能 什么是css sprite CSS spr ...
- ASP.NET Boilerplate终于发布v1.0了
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:ABP经过2年多的开发,终于发布第一个主要版本了,谨此提醒ABP的使用者. ASP.N ...
- Beginning Scala study note(6) Scala Collections
Scala's object-oriented collections support mutable and immutable type hierarchies. Also support fun ...
- VS低版本打开高版本解决方案(如08打开10、12、13版本vs编译的项目)
一.vs2005打开vs2008编译的项目:1.用记事本打开sln文件,将: Microsoft Visual Studio Solution File, Format Version 10.00 # ...
- python/matplotlib库的安装
- spring-data-redis 用法