hdu 1392 Surround the Trees
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1392
题意:给出一些点的坐标,求最小的凸多边形把所有点包围时此多边形的周长。
解法:凸包ConvexHull 模板题
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#define inf 0x7fffffff
#define PI 3.141592654
#define exp 1e-10
using namespace std;
struct Point
{
double x,y;
Point (double x=,double y=):x(x),y(y){}
friend bool operator < (Point A,Point B)
{
if (A.x != B.x) return A.x < B.x;
return A.y<B.y;
}
}an[];
typedef Point Vector;
Vector operator + (Vector A,Vector B) {return Vector(A.x+B.x , A.y+B.y); }
Vector operator - (Vector A,Vector B) {return Vector(A.x-B.x , A.y-B.y); } int dcmp(double x)
{
if (fabs(x)<exp) return ;
else return x< ? - : ;
}
double cross(Vector A,Vector B)
{
return A.x*B.y-B.x*A.y;
}
int ConvexHull(Point *p,int n,Point *ch)
{
sort(p,p+n);
int m=;
for (int i= ;i<n ;i++)
{
while (m> && dcmp(cross(ch[m-]-ch[m-] , p[i]-ch[m-]))<=) m--;
ch[m++]=p[i];
}
int k=m;
for (int i=n- ;i>= ;i--)
{
while (m>k && dcmp(cross(ch[m-]-ch[m-] , p[i]-ch[m-]))<=) m--;
ch[m++]=p[i];
}
ch[m++]=ch[];
if (n>) m--;
return m;
}
double dis(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
int n;
while (cin>>n,n)
{
for (int i= ;i<n ;i++)
scanf("%lf%lf",&an[i].x,&an[i].y);
if (n==) {printf("0.00\n");continue;}
else if (n==) {printf("%.2f\n",dis(an[],an[]));continue;}
Point bn[];
int m=ConvexHull(an,n,bn);
double sum=;
for (int i= ;i<m ;i++)
{
sum += dis(bn[i],bn[i+]);
}
printf("%.2f\n",sum);
}
return ;
}
hdu 1392 Surround the Trees的更多相关文章
- 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 凸包裸题
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 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 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 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1392 Surround the Trees(凸包*计算几何)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 这里介绍一种求凸包的算法:Graham.(相对于其它人的解释可能会有一些出入,但大体都属于这个算 ...
- 计算几何(凸包模板):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 ...
随机推荐
- c# 实现串口编程-操作LED屏幕
串口编程主要用到SerialPort这个类,主要实现对串口发送字节数组然后点阵屏显示相关信息,其实这个功能很简单下面给大家把整体思路用流程图展现如下:. 其实整体思路就如流程图.下面是整个流程图的一个 ...
- jQuery类级别插件--返回顶部,底部,去到任何部位
先引入js:<script type="text/javascript" src="jquery.js" ></script><s ...
- 火狐浏览器设置placeholder的时候记得改opacity
最近做项目的时候涉及到需要修改输入框的placeholder的字体颜色,我的CSS如下: ::-webkit-input-placeholder{ color: #c5c5c5;}::-moz-pla ...
- Python学习教程(learning Python)--2 Python简单函数设计
本节讨论Python程序设计时为何引入函数? 为何大家都反对用一堆堆的单个函数语句完成一项程序的设计任务呢? 用一条条的语句去完成某项程序设计时,冗长.不宜理解,不宜复用,而采用按功能模块划分成函数, ...
- JQUERY 判断选择器选择的对象 是否存在
判断方法: 直接选择判断,是不正确的方法,因为 $(“#id”) 不管对象是否存在都会返回 object . if($("#id")){ alert('存在'); }else{ a ...
- SQL开发 循序渐进
吉庆师傅整理: http://www.itpub.net/thread-1418150-1-1.html
- 一步一步实现Linux设备驱动的Helloworld模块
学了那么多程序语言,总是有一个Hello world开头,不禁感叹Hello world的强大.呵呵,废话少说,咋们的故事当然要从这个Hello world开始. 先查看自己OS使用的内核版本[don ...
- 在.net程序中使用System.Net.Mail来发送邮件
System.Net.Mail是微软自家提供的工具,在.net程序中可以使用该空间中的SmtpClient实例来实现邮件的发送. 使用System.Net.Mail空间与Web.config配置相配合 ...
- centos php-fpm nginx配置
移除旧的软件包:yum remove httpd* php* 安装:yum install php php-fpm yum install php-gd php-mysql php-mbstring ...
- 三种嵌入式web服务器(Boa / lighttpd / shttpd)的 linux移植笔记
一:移植Boa(web服务器)到嵌入式Linux系统 一.Boa程序的移植 1.下载Boa源码 下载地址: http://www.boa.org/ 目前最新发行版本: 0.94.13 ...