*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 ...
随机推荐
- ssl访问的原理
本文无图文对照解释,但力求通俗易懂.请读者边读边手绘各个流程,一便于理解. 总体交互流程如下 1. 客户端发起HTTPS请求 这个没什么好说的,就是用户在浏览器里输入一个https ...
- 学习CSS 笔记
1.动态修改div的大小 Html: <div> Hello </div> css: div { resize:both; overflow:auto; } 2. box-si ...
- 关于一些对map和整行读取文件操作
public static void main(String[] args) { Map<String, String> map = new HashMap<String, Stri ...
- Windows 搭建jdk、Tomcat、eclipse以及SVN、maven插件开发环境
未经允许,不得转载 Jdk1.7安装 jdk下载地址 http://www.oracle.com/technetwork/java/javase/downloads/index.html 安装jdk之 ...
- python中的collections
python中有大量的内置模块,很多是属于特定开发的功能性模块,但collections是属于对基础数据的类型的补充模块,因此,在日常代码中使用频率更高一些,值得做个笔记,本文只做主要关键字介绍,详细 ...
- CoreData __ 基本原理
操作过程 Context想要获取值,先要告诉连接器,我要什么东西 链接器再告诉store, 你给我什么东西, store去找 找到之后返回给链接器,链接器再返回给Context Co ...
- 使用dd命令备份Linux分区
为了备份分区,开始使用的是Remastersys,但最终生成的iso文件仅有几十K,应该是软件bug,且此软件不再更新,后尝试使用Linux Respin,但github一直连接不上. 其实可以尝试使 ...
- [译]:Xamarin.Android开发入门——Hello,Android Multiscreen快速上手
原文链接:Hello, Android Multiscreen Quickstart. 译文链接:Hello,Android Multiscreen快速上手 本部分介绍利用Xamarin.Androi ...
- Oracle VM VirtualBox 安装CentOS 配置图形界面记录
su yum groupinstall "X Window System" -y yum groupinstall "Desktop" -y 最后运行#star ...
- Android下的xml资源详解
最近遇到的bitmap资源 <?xml version= "1.0" encoding ="utf-8"?> <bitmap xmlns:an ...