POJ1113:Wall (凸包:求最小的多边形,到所有点的距离大于大于L)
2018-03-04 10:54
351 查看
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
Sample Output
Hint
结果四舍五入就可以了
题意:给定N个点,求用一个多边形把这些点包括进去,且每个点到多边形的距离都大于等于L。
思路:
先不考虑L这个条件,因为两点之间,直线最短,所以对于凹进去的部分,我们肯定有最短的直线可以包含它,可以忽略,所以是求凸包。
然后考虑L,对于求出的凸多边形,对于它的顶点X,可以证明每个X附近需要增加一定的圆弧来保证顶点到圆弧的距离大于等于L,
所有X的圆弧角度之和为Pi,将凸包平移与圆弧连接成封闭图案,最终 ans=凸包+2*Pi*L。
看图就知道了--->
Graham算法求凸包:
(注意需要对N讨论,此题N>=3,所以没有讨论)。
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 200
Sample Output
1628
Hint
结果四舍五入就可以了
题意:给定N个点,求用一个多边形把这些点包括进去,且每个点到多边形的距离都大于等于L。
思路:
先不考虑L这个条件,因为两点之间,直线最短,所以对于凹进去的部分,我们肯定有最短的直线可以包含它,可以忽略,所以是求凸包。
然后考虑L,对于求出的凸多边形,对于它的顶点X,可以证明每个X附近需要增加一定的圆弧来保证顶点到圆弧的距离大于等于L,
所有X的圆弧角度之和为Pi,将凸包平移与圆弧连接成封闭图案,最终 ans=凸包+2*Pi*L。
看图就知道了--->
Graham算法求凸包:
(注意需要对N讨论,此题N>=3,所以没有讨论)。
#include<cmath> #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int maxn=1010; const double pi=acos(-1.0); const double eps=1e-6; struct Cpoint { double x,y; Cpoint(){} Cpoint(double xx,double yy):x(xx),y(yy){} Cpoint friend operator -(Cpoint a,Cpoint b){ return Cpoint(a.x-b.x, a.y-b.y); } double friend operator ^(Cpoint a,Cpoint b){ return a.x*b.y-b.x*a.y; } bool friend operator <(Cpoint a,Cpoint b){ if(a.y==b.y) return a.x<b.x; return a.y<b.y; } }; double dist(Cpoint a,Cpoint b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int Sign(double x) { if(x>=-eps&&x<=eps) return 0; if(x>eps) return 1; return -1; } int N,L; Cpoint P[maxn]; bool cmp(Cpoint a,Cpoint b) { int s=Sign((a-P[1])^(b-P[1])); if(s>0||(s==0&&dist(a,P[1])<dist(b,P[1]))) return true; return false; } double Graham() //如果N<3还得讨论一下。 { double res=0; sort(P+1,P+N+1); //得到“原点 ” sort(P+2,P+N+1,cmp); //得到积角序 int q[maxn],top=3; q[1]=1; q[2]=2; q[3]=3; for(int i=4;i<=N;i++){ while(top>1&&Sign((P[q[top]]-P[q[top-1]])^(P[i]-P[q[top]]))<=0) top--; q[++top]=i; } for(int i=1;i<top;i++) res+=dist(P[q[i]],P[q[i+1]]); res=res+dist(P[q[top]],P[1])+2.0*pi*L; return res; } int main() { while(~scanf("%d%d",&N,&L)){ for(int i=1;i<=N;i++) scanf("%lf%lf",&P[i].x,&P[i].y); printf("%d\n",(int)(Graham()+0.5)); }return 0; }
相关文章推荐
- poj 1113 Wall 【求包围所有点、且与所有点距离至少为L的曲线的最小周长】
- (hdu step 7.1.7)Wall(求凸包的周长——求将所有点围起来的最小凸多边形的周长)
- poj 1584 计算几何 (点到线段距离+判断点是否在多边形内+判断多边形是否为凸包)
- 最近距离 已知平面上的若干点的位置,存入一个List中。现在需要计算所有这些点中, 距离最近的两个点间的最小距离。请补全缺失的代码。
- POJ 3608 求两凸包的最小距离 (凸包+旋转卡壳)
- 弦图判定及最小染色数 弦图:所有长度大于3的环均有弦的图叫弦
- hunnu11327(凸包,凸包边界上所有的点都看作是多边形的端点)
- Bridge Across Islands POJ - 3608 凸多边形间最小距离/旋转卡壳
- 旋转卡壳---凸多边形间最小距离
- POJ 3608 求两个凸包之间的最小距离
- 一棵排序二叉树,令 f=(最大值+最小值)/2,设计一个算法,找出距离f值最近、大于f值的结点。复杂度如果是O(n2)则不得分。
- 已知平面上若干个点的坐标。需要求出在所有的组合中,4个点间平均距离的最小值(四舍五入,保留2位小数)。
- 最近距离 已知平面上的若干点的位置,存入一个List中。现在需要计算所有这些点中,距离最近的两个点间的最小距离。
- poj1113-Wall (国王的凸包)
- 图论之遍历所有点的最小距离
- [poj1113][Wall] (水平序+graham算法 求凸包)
- (HDU 5723)Abandoned country <最小生成树 + 树上所有两点之间的距离的期望> 多校训练1
- 最小包围多边形(凸包;最小包围点集)——C代码例子
- POJ1113 Wall【凸包】
- Wall--POJ1113(极角排序+求凸包)