*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 ...
随机推荐
- python3的pickle导致乱码
资料: http://www.cnblogs.com/pzxbc/archive/2012/03/18/2404715.html http://bbs.chinaunix.net/thread-419 ...
- Latex中插入C语言代码
Latex是一个文本排版的语言,能排版出各种我们想要的效果.而且用代码排版的优点是易于修改板式,因此在文本内容的排版时,Latex应用十分广泛. 当我们需要在Latex中插入代码时,就需要用到 \us ...
- Flex中的initialize,creationComplete和applicationComp
转自:http://blog.csdn.net/sjz168/article/details/7244374 1.Application标签中有三个事件initialize,creationCompl ...
- 使用getParts()上传多个文件
<!DOCTYPE html> <html> <head> <title></title> <meta http-equiv=&quo ...
- 采用cocos2d-x lua 的listview 实现pageview的翻页效果之上下翻页效果
--翻页滚动效果local function fnScrollViewScrolling( sender,eventType) -- body if eventType == 10 the ...
- SpringMVC文件上传和下载
上传与下载 1文件上传 1.1加入jar包 文件上传需要依赖的jar包 1.2配置部件解析器 解析二进制流数据. <?xml version="1.0" encoding=& ...
- WPF如何控制每个窗体确保只打开一次
在主窗体上点击菜单时,如果做到每个窗体不会被重复打开,如果打开了,可以将其重新获得焦点. 首先在主窗体中将菜单关联的窗体实例化. 第二步:将每个菜单对应窗体的closing事件重写.之所以要重写clo ...
- [Git] 还原Git上commit,但是没有push代码
直接在Idea上操作2步解决: 1. 找到: 2. 在To Commit里面填写:HEAD^,表示将commit的信息还原为上一次的,需要多次直接reset多次即可: 使用命令行:原理一样 以下内容转 ...
- Linux下安装Tomcat服务器和部署Web应用
一.上传Tomcat服务器
- Puppet自动化运维-C/S架构概念(1)
1.Puppet工作模式 (1)采用C/S架构(即是server<=>client) (2)Master 会对自己形成自签名CA中心,对Agent端进行证书颁发,验证通过才允许Agent( ...