*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 ...
随机推荐
- SQL中循环和条件语句
.if语句使用示例: declare @a int begin print @a end else begin print 'no' end .while语句使用示例: declare @i int ...
- WinForm跨窗体传值
1.另一窗体建公共变量listdataRow public List<DataGridViewRow> listdataRow = new List<DataGridViewRow& ...
- CMS系统存储路径
CMS系统特点:前后端分离 index.html 首页文件index.php 管理后台的页面 api文件夹: 提供的接口 caches文件夹: 缓存文件 html文件夹: 生成的静态页面 phpcm ...
- android 实现返回键执行home键方法
在公司的产品开发,中老板很是执着于,不要看到启动界面,但是又不想去掉启动界面. so 实现返回键执行home键方法. public boolean onKeyDown(int keyCode, Key ...
- vmware备忘
- OOP的四个魔术方法
1 __autoload()自动包含类文件 通常会把类的定义单独写到一个文件里,要在另外的文件调用时需要引用require,但类的定义文件会很多就会造成一下问题 //1 如果包含多个类文件,需要一一引 ...
- 与你相遇好幸运,用sinopia搭建npm私服
需求: >在企业内部搭建私有npm服务器,企业开发人员上传下载自己开发的npm包 >私有npm服务器包不存在时,找npm或者taobao的镜像站点 >服务器硬盘有限,希望只缓存下载过 ...
- VirtualBox上搭建Ubuntu开发环境
软件版本: VirtualBox虚拟机:5.1.8 for Windows hosts x86/amd64 Ubuntu系统:Ubuntu 14.04.5 LTS Desktop (32-bit) ...
- XSS之xssprotect(转)
参考资料 1 跨网站脚本 http://zh.wikipedia.org/wiki/XSS 2 http://code.google.com/p/xssprotect/ 一 跨网站脚本介绍 ...
- C#排序算法小结
前言 算法这个东西其实在开发中很少用到,特别是web开发中,但是算法也很重要,因为任何的程序,任何的软件,都是由很多的算法和数据结构组成的.但是这不意味着算法对于每个软件设计人员的实际工作都是很重要的 ...