计算几何--求凸包模板--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,求出来围墙的长度. 分析:如果没有最小距离这个条件那么很容易看出来是一个凸包,然后在加上一个最小距离 ...
随机推荐
- 二维KMP - 求字符矩阵的最小覆盖矩阵 - poj 2185
Milking Grid Problem's Link:http://poj.org/problem?id=2185 Mean: 给你一个n*m的字符矩阵,让你求这个字符矩阵的最小覆盖矩阵,输出这个最 ...
- oracle触发器类型
http://www.cnblogs.com/roucheng/p/3506033.html 触发器是许多关系数据库系统都提供的一项技术.在ORACLE系统里,触发器类似过程和函数,都有声明,执行和异 ...
- iOS开发之蓝牙通讯
iOS开发之蓝牙通讯 一.引言 蓝牙是设备近距离通信的一种方便手段,在iPhone引入蓝牙4.0后,设备之间的通讯变得更加简单.相关的蓝牙操作由专门的CoreBluetooth.framework进行 ...
- 菜鸟成长进阶之——fiddler使用总结
作为一个猪拱性能的程序猿,不会使用fiddler来协助自己分析问题是万万不能的.还记得刚入职的时候老大提过的几个必须要熟练使用的工具中第一个就是fiddler.虽然接触了快一年了,但是还是一知半解的 ...
- IE Unknown runtime error
1. 在函数中使用原生的js的时候,有时在IE下会出现Unknown runtime error 火狐下正常 2. 解决办法, 将原生js改成jquery处理兼容问题 document.getElem ...
- java中使用 正则 抓取邮箱
我们来抓取豆瓣网的邮箱吧!把这个页面的所有邮箱都抓取下来 如https://www.douban.com/group/topic/8845032/: 代码如下: package cn.zhangzon ...
- AngularJs Cookie 的使用
最新在学习 AngularJs ,发现网上很难搜到 AngularJs.Cookie 教程, 就自己写篇博客,希望能帮到刚学的人. 废话不多说上代码 首先要引用 angular-cookies.js ...
- 设计模式之桥接模式(Bridge)
注:本文不属于原创,而是根据原文重新整理,原文是:我给媳妇解释设计模式:第一部分 设计模式不是基于理论发明的.相反,总是先有问题场景,再基于需求和情景不断演化设计方案,最后把一些方案标准化成“模式”. ...
- 研究jdk关于TreeMap 红黑树算法实现
因为TreeMap的实现方式是用红黑树这种数据结构进行存储的,所以呢我主要通过分析红黑树的实现在看待TreeMap,侧重点也在于如何实现红黑树,因为网上已经有非常都的关于红黑树的实现.我也看了些,但是 ...
- [Tool] 使用StyleCop验证命名规则
[Tool] 使用StyleCop验证命名规则 前言 微软的MSDN上,有提供了一份微软的命名方针,指引开发人员去建立风格一致的程序代码. http://msdn.microsoft.com/zh-t ...