hdu 1392:Surround the Trees(计算几何,求凸包周长)
Surround the Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6812 Accepted Submission(s): 2594
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.
Zero at line for number of trees terminates the input for your program.
12 7
24 9
30 5
41 9
80 7
50 87
22 9
45 1
50 7
0
给你n棵树的坐标,你要用一根绳子包围所有的树,忽略树的半径和高度,求这根绳子的最短长度。
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std; struct Point{
double x,y;
}p[],pl[];
double dis(Point p1,Point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
double xmulti(Point p1,Point p2,Point p0) //求p1p0和p2p0的叉积,如果大于0,则p1在p2的顺时针方向
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double graham(Point p[],int n) //点集和点的个数
{
int pl[];
//找到纵坐标(y)最小的那个点,作第一个点
int i;
int t = ;
for(i=;i<=n;i++)
if(p[i].y < p[t].y)
t = i;
pl[] = t;
//顺时针找到凸包点的顺序,记录在 int pl[]
int num = ; //凸包点的数量
do{ //已确定凸包上num个点
num++; //该确定第 num+1 个点了
t = pl[num-]+;
if(t>n) t = ;
for(int i=;i<=n;i++){ //核心代码。根据叉积确定凸包下一个点。
double x = xmulti(p[i],p[t],p[pl[num-]]);
if(x<) t = i;
}
pl[num] = t;
} while(pl[num]!=pl[]);
//计算凸包周长
double sum = ;
for(i=;i<num;i++)
sum += dis(p[pl[i]],p[pl[i+]]);
return sum;
}
int main()
{
int n;
while(cin>>n){
if(n==) break;
int i;
for(i=;i<=n;i++)
cin>>p[i].x>>p[i].y;
if(n==){
cout<<<<endl;
continue;
}
if(n==){
cout<<setiosflags(ios::fixed)<<setprecision();
cout<<dis(p[],p[])<<endl;
continue;
}
cout<<setiosflags(ios::fixed)<<setprecision();
cout<<graham(p,n)<<endl;
}
return ;
}
Freecode : www.cnblogs.com/yym2013
hdu 1392:Surround the Trees(计算几何,求凸包周长)的更多相关文章
- HDU 1392 Surround the Trees(几何 凸包模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1392 题目大意: 二维平面给定n个点,用一条最短的绳子将所有的点都围在里面,求绳子的长度. 解题思路: 凸包的模 ...
- HDU 1392 Surround the Trees(凸包*计算几何)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 这里介绍一种求凸包的算法:Graham.(相对于其它人的解释可能会有一些出入,但大体都属于这个算 ...
- 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 (Graham求凸包周长)
题目链接 题意 : 让你找出最小的凸包周长 . 思路 : 用Graham求出凸包,然后对每条边求长即可. Graham详解 #include <stdio.h> #include < ...
- 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 凸包模板
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(凸包入门)
Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to surround a ...
随机推荐
- CompletableFuture 详解
转 http://www.jianshu.com/p/6f3ee90ab7d3 CompletableFuture类实现了CompletionStage和Future接口.Future是Java 5添 ...
- springmvc 日期解决方案(三)使用jackson
引入jar包: <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jac ...
- 对固态硬盘ssd进行4k对齐
别让SSD成半吊子!你真的4K对齐了吗? http://ssd.zol.com.cn/537/5374950_all.html SSD固态硬盘一键分区后如何检测4K对齐? http://pcedu.p ...
- 【Leet Code】Palindrome Number
Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an ...
- 【CentOS6.5】MySQL安装和配置
1./etc/my.cnf 这是mysql的主配置文件 2.数据存放位置 3.错误存放位置
- Android中 单位 介绍
看到有很多网友不太理解dp.sp和px的区别:现在这里介绍一下dp和sp.dp也就是dip.这个和sp基本类似.如果设置表示长度.高度等属性时可以使用dp 或sp.但如果设置字体,需要使用sp.dp是 ...
- HTML5学习笔记 音频
HTML5提供了播放音频的标准. Web上的音频 直到现在,仍然不存在一项旨在网页上播放音频的标准. 今天,大多数音频是通过插件比如flash来播放的.然而,并非所有的浏览器都拥有同样的插件. hmt ...
- Linux命令-帮助命令:man
man ls 查看ls命令的帮助信息(输出结果和less命令一样,需要空格,回车,,上箭头,下箭头,PgUp,PgDn,q等方式浏览) 注意:由于这个文件信息量比较大,需要查找某一个字符,输入/,输入 ...
- 线程池c3p0和dbcp2的配置初始化实例
一.c3p0 public class ConnectionManager { public static ComboPooledDataSource dataSource; static { try ...
- Guid.NewGuid().ToString()得几种格式显示
1.Guid.NewGuid().ToString("N") 结果为: 38bddf48f43c48588e0d78761eaa1ce6 2.Guid.NewGuid ...