【poj1113】 Wall
来源:互联网 发布:osx和linux 编辑:程序博客网 时间:2024/05/29 18:35
http://poj.org/problem?id=1113 (题目链接)
题意:给定多边形城堡的n个顶点,绕城堡外面建一个围墙,围住所有点,并且墙与所有点的距离至少为L,求这个墙最小的长度。
Solution
凸包裸题。凸包的构造的话,有一篇博客写得很好,就是看着有点乱,界面太丑了= =。
很容易发现,所求的的墙的最小长度实际上就是平面凸包的周长加上以L为半径的圆的周长。这个圆是怎么来的呢,其实很好理解。对于城堡的顶点到墙的距离,想要墙尽可能短,那么一定是以顶点为圆心,L为半径的圆弧。
比如说这个,城墙的4个角就是各为90°的圆弧,很容易脑补出对于任意一个凸包,圆弧的总度数相加一定是360°。
但是这里有一个问题困扰了我很久,为什么一定要删去凸包上共线的点,不删去的话好像也没有什么影响,可是交上去就Wa。拍了下,发现原来当两个点重复的时候就Gi了= =,至于为什么,自己画个图好好想想吧。
提供一组有重复点的数据:
7 0
9 0
5 1
3 3
5 4
5 4
7 6
9 519
代码:
// poj1113#include<algorithm>#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<map>#define inf 2147483640#define LL long long#define Pi acos(-1.0)#define free(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);using namespace std;inline LL getint() { LL x=0,f=1;char ch=getchar(); while (ch>'9' || ch<'0') {if (ch=='-') f=-1;ch=getchar();} while (ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();} return x*f;}const int maxn=1010;struct point {int x,y;}p[maxn];int n,l,top,s[maxn];int Cross(point p0,point p1,point p2) { //叉乘 return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);}double Dis(point a,point b) { return sqrt((double)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}bool cmp(point a,point b) { int t=Cross(p[1],a,b); if (t>0) return 1; else if (t==0 && Dis(p[0],a)<Dis(p[0],b)) return 1; else return 0;}void Graham() { if (n==1) {top=1;s[1]=1;} else if (n==2) {top=2;s[1]=1;s[2]=2;} else { s[1]=1;s[2]=2; top=2; for (int i=3;i<=n;i++) { while (top>1 && Cross(p[s[top-1]],p[s[top]],p[i])<=0) top--; s[++top]=i; } }}int main() { while (scanf("%d%d",&n,&l)!=EOF) { int k=1; for (int i=1;i<=n;i++) { scanf("%d%d",&p[i].x,&p[i].y); if (p[i].x<p[k].x || (p[i].x==p[k].x && p[i].y<p[k].y)) k=i; } point p0=p[k]; p[k]=p[1];p[1]=p0; sort(p+2,p+1+n,cmp); Graham(); double ans=0; for (int i=1;i<top;i++) ans+=Dis(p[s[i]],p[s[i+1]]); ans+=Dis(p[s[1]],p[s[top]]); ans+=2*Pi*l; printf("%d\n",(int)(ans+0.5)); } return 0;}
0 0
- POJ1113 Wall
- poj1113 Wall
- poj1113 - Wall
- POJ1113 Wall
- POJ1113 Wall
- POJ1113-Wall
- poj1113 wall
- Poj1113 Wall
- 【poj1113】 Wall
- poj1113 Wall
- POJ1113 Wall
- POJ1113:Wall
- poj1113——Wall
- 【计算几何】poj1113 Wall
- POJ1113 Wall 凸包
- poj1113--Wall(凸包)
- [POJ1113] Wall && 计算几何
- poj1113 Wall 凸包
- 数据结构实验之数组三:快速转置
- 别管我,我要养生了,因为我老了
- C++ 11 的 unique_ptr
- CAS自定义登录
- leetcode-python 第七周
- 【poj1113】 Wall
- Android Studio Utf-8编码设置
- javaScript中Dom编程
- HDU 4460-BFS
- DjangoRestFramework实践笔记
- 威佐夫博弈
- HDU 2821--Pusher
- STL算法使用
- 数据结构与算法学习笔记——二叉树的初步理解