计算几何--求凸包模板--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,求出来围墙的长度. 分析:如果没有最小距离这个条件那么很容易看出来是一个凸包,然后在加上一个最小距离 ...
随机推荐
- ACM中的浮点数精度处理
在ACM中,精度问题非常常见.其中计算几何头疼的地方一般在于代码量大和精度问题,代码量问题只要平时注意积累模板一般就不成问题了.精度问题则不好说,有时候一个精度问题就可能成为一道题的瓶颈,让你debu ...
- DataBase --- Intellij IDEA 14.1.4使用Java连接SQL Server教程
Java连接数据库的方法大体分为两种:正向连接和反向连接.反向连接需要编译器提供相关的插件来支持,目前主流的java IDE都支持反向连接.这里主要对正向连接做一个经验总结. 一.数据库的配置 1.新 ...
- (一)Protobuf的Java使用
学习使用Protobuf,创建java文件 windows : 步骤一:两个文件:proto.exe, protobuf-Java-2.4.1.jar 步骤二:建立一个工程CreateProtoBu ...
- C#开发体感游戏 Kinect应用知识
Kinect首先是一个XBox 360外接体感设备,通过无线方式捕捉动作感知.由PrimeSense提供Range Camera技术,同类产品如任天堂Wii.Play Station Move,必须让 ...
- sencha/extjs 动态创建grid表格
//创建普通表格 id,父容器,标题,json数据字符串,列名(逗号分隔),json数据key即store的fields属性(逗号分隔) function createCommonTable(id, ...
- php中的常用数组函数(一)(比较多个数组的差集的函数们 array_diff_assoc() array_diff() array_diff_key() array_diff_ukey() array_diff_uassoc())
array_diff_assoc($arr1, $arr2, $arr3,... n); 返回:一个$arr1的副本,后续的数组中出现一个键值相同的元素,就在副本中删掉这个元素,最后返回这个副本. 如 ...
- angularjs post
/** * POST 1 * $http.post('http://localhost:8001/quickstart/task/create', { newTask: newTask }) */ / ...
- JVM的ClassLoader过程分析
本文来自网络:深入分析Java ClassLoader原理 http://my.oschina.net/zhengjian/blog/133836 一. JVM的ClassLoader过程以及装载原理 ...
- inner Join on 随随随随随便一记
幼儿园大班生(随便的记一记) JOIN 分为:内连接(INNER JOIN).外连接(OUTER JOIN).其中,外连接分为:左外连接( ...
- CSS选择器特殊性与重要性
特殊性 在编写CSS代码的时候,我们会出现多个样式规则作用于同一个元素的情况,例如 <!-- HTML --> <header> <nav class="nav ...