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 ...
随机推荐
- linux 下处理大文件
.head tail more .先把大文件进行分割 split split 参数: -a, --suffix-length=N 指定输出文件名的后缀,默认为2个 -b, --bytes=SIZE 指 ...
- JDBC JdbcUtils( 本博多次出现的简陋工具类)
package test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; ...
- ubuntu 将"/TMP"挂载到内存中
一.修改"/etc/fstab"文件: sudo gedit /etc/fstab 二.在文件中添加下列语句: 1, # / was on /dev/s ...
- Linux命令-文件搜索命令:grep
选项: -A 数字:表示查看满足条件行后的N条记录 -B 数字:表示查看满足条件行前的N条记录 -C 数字:表示查看满足条件行前后各N条记录 more /etc/inittab 分页查看inittab ...
- java调试打断点和不打断点执行结果不一致问题解决
java程序在调试的时候需要debug来跟踪一下结果,有一种情况是这样的,正常执行java程序就会出现问题,而断点debug跟踪此方法的时候却是正常的,不断测试结果都是这样,由此判断有可能是因为此方法 ...
- Vue中正确使用jQuery的方法
题主Vue小白,入门demo时想在其中使用jQuery(当然可能是不推荐的做法哈,毕竟俩儿的风格不一样,但万一你就需要呢 _^ ^_),结果遇到问题,最终倒腾解决. 编译报错:$ is undefin ...
- Tomcat 文件夹结构
文件夹 描写叙述 /bin ...
- C语言的工具集
1. lint可以对c程序进行更加广泛的错误分析,lint不是缩写,它的命名是因为它像在程序中“吹毛求疵”,现在许多linux发行版都包括了它的增强版splint(Secure Programming ...
- C# 文件与二进制互转数据库写入读出
//这个方法是浏览文件对象 private void button1_Click(object sender, EventArgs e) { //用户打开文件浏览 using (OpenFileDia ...
- 5.1 Zend_Log_Writer
22.2.2. 写入到数据库 22.2.3. 踩熄Writer 22.2.4. 測试 Mock 22.2.5. 组合Writers