*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 ...
随机推荐
- Android studio 英文——中文 翻译插件
TranslationPlugin 1.手动下载 TranslationPlugin ,在Android studio 中 2. 3. 4.选中文件,点击OK 5.设置快捷键 代号1 : 代号2 :
- 7.iOS Notification
1. 通知中心认识 通知中心实际上是在程序内部提供了消息广播的一种机制.通知中心不能在进程间进行通信,它只能进行程序内部通信,不能跨应用程序进程通信.通知中心,当通知中心接受到消息后会根据设置,根据内 ...
- Ruby混合类型
当一个类拥有可以从多个父类继承的特点,类应该显示多重继承. Ruby没有直接支持多继承,但Ruby模块有另一个精彩使用.他们几乎消除多重继承的需要,提供了一个工厂,称为混入. 混合类型给一个精彩的控制 ...
- quartz配置文件详解
quartz配置文件详解(转载) quartz学习总结: 一.关于job: 用Quartz的行话讲,作业是一个执行任务的简单Java类.任务可以是任何Java代码.只需你实现org.qu ...
- 前端工具gulp使用
一.构建gulp环境 1.下载nodejs gulp基于node.js,要通过nodejs的npm安装gulp,所以要先安装node.js环境.(英文官网/中文官网链接). 通过cmd命令窗口确定安装 ...
- FragmentPagerAdapter加载fragment并使用setUserVisibleHint()处理预加载时遇到的坑,给textview赋值时出现的空指针异常
FragmentPagerAdapter加载fragment并使用setUserVisibleHint()处理预加载时,给textview赋值时出现的空指针异常 public class BaseFr ...
- MongoDB聚合运算之group和aggregate聚集框架简单聚合(10)
聚合运算之group 语法: db.collection.group( { key:{key1:1,key2:1}, cond:{}, reduce: function(curr,result) { ...
- 解决apache 443端口被占用
检测 错误 httpd.exe -w -n "apache" -k start , 进入Apache的安装目录, 搜索httpd-ssl.conf,httpd-ahssl.conf ...
- HTML5之文件API
问题很简单,做个上传文件的页面. <!-- multiple代表可上传多个文件 --> <input type="file" id="file" ...
- 采用Lambda表达式快速实现实体模型对象转换到DTO
在项目中,采用code first时建立的模型对象不能直接用于数据传输,需要从新根据需求建立Dto对象 为什么需要建立Dto对象呢? DTO即数据传输对象.之前不明白有些框架中为什么要专门定义DTO来 ...