计算几何--求凸包模板--Graham算法--poj 1113
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 28157 | Accepted: 9401 |
Description
towers. Instead, he ordered to build the wall around the whole castle using the least amount of stone and labor, but demanded that the wall should not come closer to the castle than a certain distance. If the King finds that the Architect has used more resources
to build the wall than it was absolutely necessary to satisfy those requirements, then the Architect will loose his head. Moreover, he demanded Architect to introduce at once a plan of the wall listing the exact amount of resources that are needed to build
the wall.

Your task is to help poor Architect to save his head, by writing a program that will find the minimum possible length of the wall that he could build around the castle to satisfy King's requirements.
The task is somewhat simplified by the fact, that the King's castle has a polygonal shape and is situated on a flat ground. The Architect has already established a Cartesian coordinate system and has precisely measured the coordinates of all castle's vertices
in feet.
Input
the castle.
Next N lines describe coordinates of castle's vertices in a clockwise order. Each line contains two integer numbers Xi and Yi separated by a space (-10000 <= Xi, Yi <= 10000) that represent the coordinates of ith vertex. All vertices are different and the sides
of the castle do not intersect anywhere except for vertices.
Output
are not invented yet. However, you must round the result in such a way, that it is accurate to 8 inches (1 foot is equal to 12 inches), since the King will not tolerate larger error in the estimates.
Sample Input
9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200
Sample Output
1628
Hint
Source
1.将各点排序(请参看基础篇),为保证形成圈,把P0在次放在点表的尾部;
2.准备堆栈:建立堆栈S,栈指针设为t,将0、1、2三个点压入堆栈S;
3.对于下一个点i
只要S[t-1]、S[t]、i不做左转
就反复退栈;
将i压入堆栈S
4.堆栈中的点即为所求凸包;
代码:
#include "iostream" //poj 1113
#include "cstdio"
#include "cmath"
#include "cstring"
#include "iomanip"
#include "algorithm"
using namespace std; const double eps = 1e-8;
const double PI = acos(-1.0); int cmp(double x){ //判断一个实数的正负
if(fabs(x) < eps) return 0;
if(x > 0) return 1;
return -1;
} inline double sqr(double x){ //计算一个数的平方
return x*x;
} struct point{
double x,y;
point(){};
point(double a,double b) : x(a),y(b) {}
friend point operator - (const point &a,const point &b){
return point(a.x - b.x,a.y - b.y);
}
double norm(){
return sqrt(sqr(x) + sqr(y));
}
}; double det(const point &a,const point &b){ //计算两个向量的叉积
return a.x*b.y - a.y*b.x;
} double dist(const point &a,const point &b){ //计算两个点的距离
return (a-b).norm();
} #define N 1005 point start;
point stacks[3*N]; bool cmp_sort(point a,point b) //对点进行极角排序(以左下角的点为参考点)
{
int temp = cmp(det(a-start,b-start));//(叉积判断)
if(temp==1) return true;
else if(temp==-1) return false;
temp = cmp(dist(b,start)-dist(a,start)); //sort排序后只有true和false,牢记!!!
if(temp==1) return true;
return false;
} void Melkman(point *a,int &n) //求n个点的凸包
{
int k; //记录左下点的下标
int i,j;
point temp;
double minf = 100005;
for(i=0; i<n; ++i)
{
if(cmp(a[i].x-minf) == -1)
{
minf = a[i].x;
k = i;
}
else if(cmp(a[i].x-minf) == 0)
{
if(cmp(a[k].y-a[i].y) == 1)
k = i;
}
}
{ //交换点
temp = a[0];
a[0] = a[k];
a[k] = temp;
}
start = a[0];
sort(a+1,a+n,cmp_sort); //排序!
for(i=0; i<N; ++i)
stacks[i].x = stacks[i].y = 0;
stacks[0] = a[0];
stacks[1] = a[1];
int top=1;
i = 2;
while(i<n)
{
if(top==0 || cmp(det(stacks[top-1]-stacks[top],stacks[top]-a[i]))>=0)
{
top++;
stacks[top] = a[i];
i++;
}
else
top--;
}
for(n=0; n<=top; ++n)
a[n] = stacks[n];
} int main()
{
int i,j;
int n,r;
point a[N];
while(scanf("%d %d",&n,&r)!=-1)
{
for(i=0; i<n; ++i)
scanf("%lf %lf",&a[i].x,&a[i].y);
Melkman(a,n);
double ans = 2*PI*r;
a[n] = a[0]; //将n个点连成一个圈
for(i=0; i<n; ++i)
ans += dist(a[i],a[i+1]);
printf("%.0lf\n",ans);
}
return 0;
}
计算几何--求凸包模板--Graham算法--poj 1113的更多相关文章
- 凸包模板——Graham扫描法
凸包模板--Graham扫描法 First 标签: 数学方法--计算几何 题目:洛谷P2742[模板]二维凸包/[USACO5.1]圈奶牛Fencing the Cows yyb的讲解:https:/ ...
- 计算几何(凸包模板):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 ...
- POJ-3348 Cows 计算几何 求凸包 求多边形面积
题目链接:https://cn.vjudge.net/problem/POJ-3348 题意 啊模版题啊 求凸包的面积,除50即可 思路 求凸包的面积,除50即可 提交过程 AC 代码 #includ ...
- Graham求凸包模板
struct P { double x, y; P(, ):x(x), y(y) {} double add(double a, double b){ ; return a+b; } P operat ...
- POJ-1113 Wall 计算几何 求凸包
题目链接:https://cn.vjudge.net/problem/POJ-1113 题意 给一些点,求一个能够包围所有点且每个点到边界的距离不下于L的周长最小图形的周长 思路 求得凸包的周长,再加 ...
- poj1113Wall 求凸包周长 Graham扫描法
#include<iostream> #include<algorithm> #include<cmath> using namespace std; typede ...
- POJ 1113 凸包模板题
上模板. #include <cstdio> #include <cstring> #include <iostream> #include <algorit ...
- HDU 4667 Building Fence(2013多校7 1002题 计算几何,凸包,圆和三角形)
Building Fence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)To ...
- Wall - POJ 1113(求凸包)
题目大意:给N个点,然后要修建一个围墙把所有的点都包裹起来,但是要求围墙距离所有的点的最小距离是L,求出来围墙的长度. 分析:如果没有最小距离这个条件那么很容易看出来是一个凸包,然后在加上一个最小距离 ...
随机推荐
- 字符串hash - POJ 3461 Oulipo
Oulipo Problem's Link ---------------------------------------------------------------------------- M ...
- 译:在ASP.NET MVC5中如何使用Apache log4net 类库来记录日志
译文出处:http://www.codeproject.com/Articles/823247/How-to-use-Apache-log-net-library-with-ASP-NET-MVC 在 ...
- Can you explain Lazy Loading?
Introduction Lazy loading is a concept where we delay the loading of the object until the point wher ...
- 利用PBfunc在Powerbuilder中使用https获取微信的AccessToken
在前篇中讲解了使用PBFunc在Powerbuilder自己进行http的GET和POST操作. 本篇简单用代码演示下https的微信AccessToken的获取: n_pbfunc_http lnv ...
- Visual Studio 2013下JSON可视化工具
Visual Studio 2013现在我们有个小工具可以实现JSON可视化,这样给我们调试JSON提供了便利. JSON这种数据格式已经比较流行,在WEB前端随处可见. 在你需要安装VS ...
- 【翻译】配置RSVP-signaled LSP
源地址: https://www.juniper.net/techpubs/software/junos-security/junos-security10.2/junos-security-swco ...
- andriod 动态创建控件
Button btNext=(Button)findViewById(R.id.next); btNext.setOnClickListener(new Button.OnClickListener( ...
- Java或Android开发中,去掉块注释格式化后每行出现的星号(*)的解决方案。(Eclipse)
找到子项,在 Window->Prefrences->Java->Code Style->Formatter,点击New新建 Active profile,然后在Comment ...
- 自学 iOS – 三十天三十个 Swift 项目
自学 iOS – 三十天三十个 Swift 项目 github源码地址:https://github.com/allenwong/30DaysofSwift
- CocoaPods的使用(图文并茂)OS X 10.11 系统
系统:OS X EI Capitan 版本:10.11.2 开发工具:XCode:7.2 要使用CocoaPods,那么就需要先安装哦,你安装了么?如果没安装那就请阅读我的前篇<OS X 10. ...