Surround the Trees HDU 1392 凸包
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
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 109
#define N 21
#define MOD 1000000
#define INF 1000000009
const double eps = 1e-;
const double PI = acos(-1.0);
/*
所有线段投射到给定线段上取交集,如果交集距离大于eps 存在!s
*/
int sgn(double x)
{
if (fabs(x) < eps) return ;
if (x < ) return -;
else return ;
}
struct Point
{
double x, y;
Point() {}
Point(double _x, double _y) :x(_x), y(_y) {}
Point operator - (const Point& r)const
{
return Point(x - r.x, y - r.y);
}
double operator ^(const Point& r)const
{
return x*r.y - y*r.x;
}
double operator * (const Point& r)const
{
return x*r.x + y*r.y;
}
};
double dist(Point a, Point b)
{
return sqrt((a - b)*(a - b));
}
struct Line
{
Point s, e;
Line() {}
Line(Point _a, Point _B) :s(_a), e(_B) {}
};
bool Seg_inter_line(Line l1, Line l2)
{
return sgn((l2.s - l1.e) ^ (l1.s - l1.e))*sgn((l2.e - l1.e) ^ (l1.s - l1.e)) <= ;
}
bool cross(Line l1, Line l2)
{
return
max(l1.s.x, l1.e.x) >= min(l2.s.x, l2.e.x) &&
max(l2.s.x, l2.e.x) >= min(l1.s.x, l1.e.x) &&
max(l1.s.y, l1.e.y) >= min(l2.s.y, l2.e.y) &&
max(l2.s.y, l2.e.y) >= min(l1.s.y, l1.e.y) &&
sgn((l2.s - l1.e) ^ (l1.s - l1.e))*sgn((l2.e - l1.e) ^ (l1.s - l1.e)) <= &&
sgn((l1.s - l2.e) ^ (l2.s - l2.e))*sgn((l1.e - l2.e) ^ (l2.s - l2.e)) <= ;
}
double CalcArea(Point p[], int n)
{
double res = ;
for (int i = ; i < n; i++)
res += (p[i] ^ p[(i + ) % n]) / ;
return fabs(res);
}
double CalcLen(Point p[],int n)
{
double res = ;
for (int i = ; i < n; i++)
res += dist(p[i], p[(i + ) % n]);
return (res);
}
bool isconvex(Point p[], int n)
{
bool s[];
memset(s, false, sizeof(s));
for (int i = ; i < n; i++)
{
s[sgn((p[(i + ) % n] - p[i]) ^ (p[(i + ) % n] - p[i])) + ] = true;
if (s[] && s[])
return false;
}
return true;
}
//Point Calgravitycenter(Point p[], int n)
//{
// Point res(0, 0);
// double area = 0;
// for (int i = 0; i < n; i++)
// {
// ci[i] = (p[i] ^ p[(i + 1) % n]);
// ti[i].x = (p[i].x + p[(i + 1) % n].x);
// ti[i].y = (p[i].y + p[(i + 1) % n].y);
// res.x += ti[i].x * ci[i];
// res.y += ti[i].y * ci[i];
// area += ci[i] / 2;
// }
// res.x /= (6 * area);
// res.y /= (6 * area);
// return res;
//}
Point L[MAXN],tmp[MAXN];
int Stack[MAXN], top;
bool cmp(Point p1, Point p2)
{
double tmp = (p1 - L[]) ^ (p2 - L[]);
if (sgn(tmp) > )
return true;
else if (sgn(tmp) == && sgn(dist(p1, L[]) - dist(p2, L[]) <= ))
return true;
else
return false;
}
double Graham(int n)
{
Point p0;
int k = ;
p0 = L[];
for (int i = ; i < n; i++)
{
if ((p0.y > L[i].y) || (p0.x == L[i].x&&p0.x > L[i].x) )
{
k = i, p0 = L[i];
}
}
swap(L[k], L[]);
sort(L + , L + n, cmp);
if (n == )
{
top = , Stack[] = ;
return 0.0;
}
else if (n == )
{
top = , Stack[] = , Stack[] = ;
return dist(L[],L[]);
}
Stack[] = , Stack[] = , top = ;
for (int i = ; i < n; i++)
{
while (top > && sgn((L[Stack[top - ]] - L[Stack[top - ]]) ^ (L[i] - L[Stack[top - ]])) <= )
{
top--;
}
Stack[top++] = i;
}
double res = ;
for (int i = ; i < top; i++)
res += dist(L[Stack[i]], L[Stack[(i + ) % top]]);
return res;
}
int main()
{
int n;
while (scanf("%d", &n), n)
{
for (int i = ; i < n; i++)
scanf("%lf%lf", &L[i].x, &L[i].y);
printf("%.2lf\n", Graham(n));
}
}
Surround the Trees HDU 1392 凸包的更多相关文章
- HDU 1392 凸包模板题,求凸包周长
1.HDU 1392 Surround the Trees 2.题意:就是求凸包周长 3.总结:第一次做计算几何,没办法,还是看了大牛的博客 #include<iostream> #inc ...
- HDU 1392 凸包
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 题目大意: 二维平面给定n个点,用一条最短的绳子将所有的点都围在里面,求绳子的长度. 解题思路: 凸包的模 ...
- hdu 1392凸包周长
//用的自己的计算几何模板,不过比较慢嘿嘿 //要注意只有一个点和两个点 //Computational Geometry //by kevin_samuel(fenice) Soochow Univ ...
- HDU - 1392 凸包求周长(模板题)【Andrew】
<题目链接> 题目大意: 给出一些点,让你求出将这些点全部围住需要的多长的绳子. Andrew算法 #include<iostream> #include<cstdio& ...
- 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 ...
随机推荐
- PCB MS SQL 标量函数与表值函数(CLR) 实现文件与目录操作
一.C#写SQL SERVER(CLR)实现文件操作 标量函数: 文件移动 ,复制,检测文件存在,写入新文件文本,读取文本,创建目录,删除目录,检测目录是否存在 /// <summary> ...
- Java的Thread.currentThread().getName() 和 this.getName() 以及 对象.getName()区别???
最近在看Java多线程这本书,但是发现里面有个概念自己搞不清楚.就是Thread.currentThread().getName() 和 this.getName() 以及 对象.getName()区 ...
- weak看iOS面试
2013年 面试官:代理用weak还是strong? 我 :weak . 面试官:明天来上班吧 2014年 面试官:代理为什么用weak不用strong? 我 : 用strong会造成循环引用. 面试 ...
- MyEclipse找不到install new software
Window->Preferences->Capabilities-> classic update(勾选即可) 勾选后会出现software updates,下面按照help-&g ...
- 去掉myeclipse的预览窗口
1,选择菜单: windows -> preferences2,在弹出窗口中选择General-> Editors -> FileAssociations3,在上方框内选择*.jsp ...
- Criteria 查询
Criteria.Criterion接口和Expression类组成,他支持在运行时动态生成查询语句. Criteria查询是Hibernate提供的一种查询方式 Hibernate检索方式: PO ...
- Scala-基础-变量与常量
import junit.framework.TestCase import org.junit.Test //变量 //var 代表变量 //val 代表常量 //关键字 class,extends ...
- 研磨JavaScript系列(一):回归简单
想要理解JavaScript,你得首先放下对象和类的概念,回到数据和代码的本原.编程世界只有数据和代码两种基本元素,而这两种元素又有着纠缠不清的关系.JavaScript就是把数据和代码都简化到最原始 ...
- WEB开发模式浅析
WEB技术随着互联网的崛起而崛起,又随着移动互联网的发展而呈现更加多样化的趋势. 黑暗时代:大约在2005年以前,所谓的WEB开发主要还是美工的活,HTML/CSS占主导,Dreamwaver做为页面 ...
- 【译】x86程序员手册13-第5章 内存管理
Chapter 5 Memory Management 内存管理 The 80386 transforms logical addresses (i.e., addresses as viewed b ...