POJ 1113 Wall 求凸包的两种方法
Wall
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31199 Accepted: 10521 Description
Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he would not listen to his Architect's proposals to build a beautiful brick wall with a perfect shape and nice tall 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 first line of the input file contains two integer numbers N and L separated by a space. N (3 <= N <= 1000) is the number of vertices in the King's castle, and L (1 <= L <= 1000) is the minimal number of feet that King allows for the wall to come close to 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
Write to the output file the single number that represents the minimal possible length of the wall in feet that could be built around the castle to satisfy King's requirements. You must present the integer number of feet to the King, because the floating numbers 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 200Sample Output
1628Hint
结果四舍五入就可以了Source
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#define EPS 1e-8
#define PI 3.1415926535897932384626433832795
using namespace std;
struct point{
double x, y;
};
const int maxn = ;
point p[maxn];
int process[maxn];
int n, l, k;
bool cmp(const point p1, const point p2)
{
return (p1.y == p2.y && p1.x < p2.x || p1.y < p2.y);
}
int sgn(double x)
{
if (fabs(x) < EPS)
return ;
return x < ? - : ;
}
double get_direction(point p1, point p2, point p3)//p1p3在p1p2左侧的时候返回小于0的数
{
return ((p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y));
}
double get_distance(point p1, point p2)
{
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}
void jarvis()//卷包裹法求凸包
{
int cur = , optimal;//optimal保存最优的点,也就是最外面的点,cur是当前的
process[k++] = ;
for (int i = ; i < n; i++)
{
for (int j = ; j < n; j++)
{
if (j != cur)
{
optimal = j;
break;
}
}
for (int j = ; j < n; j++)
{
if (j != optimal && cur != j && sgn(get_direction(p[cur], p[j], p[optimal])) <= )
{
if (sgn(get_direction(p[cur], p[j], p[optimal]) == ))
{
if (get_distance(p[cur], p[j]) > get_distance(p[cur], p[optimal]))
optimal = j;
}
else
optimal = j;//严格左侧
}
}
if (optimal == )
break;
process[k++] = optimal;
cur = optimal;
}
}
int main()
{
while (~scanf("%d %d", &n, &l))
{
k = ;
for (int i = ; i < n; i++)
scanf("%lf %lf", &p[i].x, &p[i].y);
sort(p, p + n, cmp);
jarvis();
double ans = ;
for (int i = ; i <= k; i++)
{
ans += get_distance(p[process[i - ]], p[process[i % n]]);
}
ans += * PI * l;
printf("%d\n", (int)(ans + 0.5));
} return ;
}
方法二:Graham-Scan方法
这个方法的主要思想来自极坐标排序,和那个卷包裹法也差不了多少,都是找最外面的那个点,不过这个用的是如果加进来的点右拐的话,就弹出栈顶的点,也就是上一个加进来的点,知道弹到左拐为止,如果左拐直接压栈,这个博客讲的比较好http://www.cnblogs.com/Booble/archive/2011/03/10/1980089.html#2065991,刚开始极角排序的方法理解了,但是平面坐标x,y的一直不理解,因为极角排完序之后一遍for就完事了,很好理解,但是关于平面坐标x, y的方法不太理解,还有就是排序方式也不一样,后来在某篇博客上看到原来一遍只是找到了凸包的一半,虽然是遍历完了,但是,有好多点没有加入,这个它的排序特性所决定的。如果按照y的大小优先排列,那么先找的就是上面的半个凸壳,然后才是下面的凸壳,所以需要两个,这个我是理解了好久了,如果不太理解的话自动动手试试比较好
/*************************************************************************
> File Name: poj_1113_graham_coordinate.cpp
> Author: Howe_Young
> Mail: 1013410795@qq.com
> Created Time: 2015年04月15日 星期三 21时32分07秒
************************************************************************/
/*平面坐标法求凸包*/
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cstdio>
#define EPS 1e-8
#define PI 3.1415926535
using namespace std;
typedef long long LL;
struct point{
double x, y;
};
const int maxn = ;
point p[maxn];
int n, l, top, convex[maxn];//convex当做栈来使用
bool cmp(const point p1, const point p2)//比较函数,不用极角,用坐标(x, y);
{
return ((p1.y == p2.y && p1.x < p2.x) || p1.y < p2.y);
}
int sgn(double x)
{
if (fabs(x) < EPS)
return ;
return x < ? - : ;
}
double get_direction(point p1, point p2, point p3)
{
return ((p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y));
}
double get_distance(point p1, point p2)
{
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}
void Graham()
{
top = ;
//找出凸包的下半部分凸壳
for (int i = ; i < n; i++)
{
while (top > && sgn(get_direction(p[convex[top - ]], p[convex[top - ]], p[i])) > )
top--;
convex[top++] = i;
}
int tmp = top;
//找出凸包的上半部分,因为我的比较函数是写的y优先的,所以上下部分,当然也可以以x优先排序,这时候就是左右部分了
for (int i = n - ; i >= ; i--)
{
while (top > tmp && sgn(get_direction(p[convex[top - ]], p[convex[top - ]], p[i])) > )
top--;
convex[top++] = i;
}
}
int main()
{
while (~scanf("%d %d", &n, &l))
{
for (int i = ; i < n; i++)
{
scanf("%lf %lf", &p[i].x, &p[i].y);
}
sort(p, p + n, cmp);
Graham();
double ans = ;
for (int i = ; i < top - ; i++)
{
ans += get_distance(p[convex[i]], p[convex[i + ]]);
}
ans += * PI * l;
printf("%d\n", (int)(ans + 0.5));
}
return ;
}
Graham算法,极角排序方法
这个方法是真的按照极角排序来做得,只用一遍的for就行了。只不过排序的时候稍微麻烦点,其他的挺好理解的
/*极角排序方法*/
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cstdio>
#define EPS 1e-8
#define PI 3.1415926535
#define INF 99999999
using namespace std;
struct point{
double x, y;
};
const int maxn = ;
point p[maxn], pp;//pp保存最小的按个点,就是最左下角的
int n, l, top, convex[maxn];//convex栈
int sgn(double x)
{
if (fabs(x) < EPS)
return ;
return x < ? - : ;
}
double get_direction(point p1, point p2, point p3)
{
return ((p3.x - p1.x) * (p2.y - p1.y) - (p2.x - p1.x) * (p3.y - p1.y));
}
double get_distance(point p1, point p2)
{
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y));
}
bool cmp(const point p1, const point p2)//极角排序比较函数,以最左下角的点为参考点
{
if (sgn(get_direction(pp, p1, p2)) < )
return true;
if (sgn(get_direction(pp, p1, p2)) == && get_distance(pp, p1) < get_distance(pp, p2))//同样的极角,距离小的排在距离大的前头
return true;
return false;
}
void graham()//Graham-Scan算法
{
sort(p, p + n, cmp);//先排序
top = ;
if (n == )
return;
convex[] = ;
if (n == )
return;
convex[] = ;
for (int i = ; i < n; i++)//找剩下的n - 2个元素
{
while (top > && sgn(get_direction(p[convex[top - ]], p[convex[top]], p[i])) >= )//如果不满足左拐的条件,弹栈
top--;
convex[++top] = i;//将这个点压到栈中
}
}
int main()
{
while (~scanf("%d %d", &n, &l))
{
p.x = pp.y = INF;
for (int i = ; i < n; i++)
{
scanf("%lf %lf", &p[i].x, &p[i].y);
if (p[i].x < pp.x || (p[i].x == pp.x && p[i].y < pp.y))
{
pp.x = p[i].x; pp.y = p[i].y;
}
}
graham();
double ans = ;
++top;
for (int i = ; i < top; i++)
{
ans += get_distance(p[convex[i]], p[convex[(i + )%top]]);
}
ans += * PI * l;
printf("%d\n", (int)(ans + 0.5));
}
return ;
}
POJ 1113 Wall 求凸包的两种方法的更多相关文章
- POJ 1113 Wall 求凸包
http://poj.org/problem?id=1113 不多说...凸包网上解法很多,这个是用graham的极角排序,也就是算导上的那个解法 其实其他方法随便乱搞都行...我只是测一下模板... ...
- Python3求笛卡尔积的两种方法
[本文出自天外归云的博客园] 电影异次元杀阵三部曲中密室线索反复出现笛卡尔积的运用.百度百科: 笛卡尔乘积是指在数学中,两个集合X和Y的笛卡尓积(Cartesian product),又称直积,表示为 ...
- Wall - POJ 1113(求凸包)
题目大意:给N个点,然后要修建一个围墙把所有的点都包裹起来,但是要求围墙距离所有的点的最小距离是L,求出来围墙的长度. 分析:如果没有最小距离这个条件那么很容易看出来是一个凸包,然后在加上一个最小距离 ...
- POJ 1113 Wall【凸包周长】
题目: http://poj.org/problem?id=1113 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- POJ 1113 Wall (凸包)
Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall ...
- POJ 1113 Wall(计算几何の凸包)
Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall ...
- 区间求mex的两种方法
区间求mex的两种方法 1.莫队+分块/莫队+二分+树状数组 2.线段树 预处理1-i的sg值(用一个vis数组,一个cur指针) 预处理nxt数组(a[nxt[i]]=a[i]) 枚举左端点l, 考 ...
- POJ 1113 Wall(凸包)
[题目链接] http://poj.org/problem?id=1113 [题目大意] 给出一个城堡,要求求出距城堡距离大于L的地方建围墙将城堡围起来求所要围墙的长度 [题解] 画图易得答案为凸包的 ...
- 题解报告:poj 1113 Wall(凸包)
Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall ...
随机推荐
- 安卓AVD使用建议
问题描述:之前在安装了Android开发环境后,一开始并没有直接在Android手机和平板上进行调试,是使用的AVD模拟器工具.由于电脑的配置不是特别好,总感觉AVD的使用速度太慢,包括启动的时候还有 ...
- 搭建BCE本地开发环境
1. 在官网下载VirtualBox & 虚拟机 http://bce.baidu.com/doc/BAE/GUIGettingStarted.html#.E4.B8.8B.E8.BD.BD. ...
- Android 中文 API (29) —— CompoundButton
前言 本章内容是android.widget.CompoundButton,翻译来自德罗德,再次感谢德罗德 !期待你一起参与Android API 的中文翻译,联系我over140@gmail.com ...
- 【转】Spring注解详解
http://blog.csdn.net/xyh820/article/details/7303330/ 概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类 ...
- winform 对话框控件,打印控件
1.文件对话框(FileDialog) 它又常用到两个: 打开文件对话框(OpenFileDialog) 保存文件对话框(SaveFileDialog) 2.字体对话框(FontDialog) 3.颜 ...
- 学习 html5 需要学习的 基本 库
jquery backbone bootstrap underscore appframework require.js
- Unity NGUI 网络斗地主 -制作图集 Atlas
Unity NGUI 网络斗地主 -制作图集 Atlas by @杨海龙 开发环境 Win7+Unity4.2.1f4+NGUI 3.0.4版本 这一节告诉大家如何制作(图集)Atlas! 1.首 ...
- UpdateLayeredWindow是炫效果的关键
自绘——是的,输入框每个字都自己绘制,计算行宽,行高,模拟光标闪烁,处理输入法的各种事件,以及选中,拖动等功能. 支持支持一下,实际上无句柄的,就是多行富文本编辑比较麻烦,其他的,都不复杂.很容易实现 ...
- Java this 心得
用类名定义一个变量的时候,定义的应该只是一个引用,外面可以通过这个引用来访问这个类里面的属性和方法,那们类里面是否也应该有一个引用来访问自己的属性和方法纳?呵呵,JAVA提供了一个很好的东西,就是 t ...
- Finding Nemo(bfs)
Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 6988 Accepted: 1600 Description Nemo ...