计算几何--求凸包模板--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,求出来围墙的长度. 分析:如果没有最小距离这个条件那么很容易看出来是一个凸包,然后在加上一个最小距离 ...
随机推荐
- WebApi 登录身份验证
前言:Web 用户的身份验证,及页面操作权限验证是B/S系统的基础功能,一个功能复杂的业务应用系统,通过角色授权来控制用户访问,本文通过Form认证,Mvc的Controller基类及Action的权 ...
- VS2013 编译程序时提示 无法查找或打开 PDB 文件
"Draw.exe"(Win32): 已加载"C:\Users\YC\Documents\Visual Studio 2013\Projects\Draw\Debug\ ...
- CSS3属性transform详解
在CSS3中,可以利用transform功能来实现文字或图像的旋转.缩放.倾斜.移动这四种类型的变形处理,本文将对此做详细介绍. 一.旋转 rotate 用法:transform: rotate(45 ...
- sql联合查询去除重复计算总和
1.首先来个联合查询 SELECT 字段1, 字段2, 字段3, 字段4 FROM 表1 INNER JOIN 表2 ON 表1.字段x = 表2.字段x x:代表随意的一个,只要在联合查询的两张表都 ...
- 验证控件插图扩展控件ValidatorCalloutExtender(用于扩展验证控件)和TextBoxWatermarkExtender
<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptMan ...
- LeetCode129:Sum Root to Leaf Numbers
题目: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a nu ...
- uml中的几种关系
这是一堂关于UML基础知识的补习课:现在我们做项目时间都太紧了,基本上都没有做过真正的class级别的详细设计,更别提使用UML来实现规范建模了:本篇主要就以前自己一直感觉很迷糊的几种class之间的 ...
- long(Long)与int(Integer)之间的转换
最近由于在做一个众筹的项目,其中有一个查找项目支持数的接口,查找的方法定义的是一个long型的,我更新项目中的支持数的时候是int型的,所以需要在long型与int型之间转化,下面把转转化的详细方法记 ...
- [Architecture Design] 3-Layer基础架构
[Architecture Design] 3-Layer基础架构 三层式体系结构 只要是软件从业人员,不管是不是本科系出身的,相信对于三层式体系结构一定都不陌生.在三层式体系结构中,将软件开发所产出 ...
- [Angularjs]视图和路由(四)
写在前面 关于angularjs的路由的概念基本上这篇就要结束了,通过学习,以及在实际项目中的实践,还是比较容易上手的.自己也通过angularjs做了一个在app上的一个模块,效果还是可以的. 系列 ...