POJ 1113 Wall 【凸包入门】
2012-09-01 09:57
465 查看
原题连接:http://poj.org/problem?id=1113
题意:给 n 个点,你见一个围墙,使所有的点到围墙的距离不小于 l 。求围墙长度。。
思路:先求出n个点的 凸包,然后根据凸包建一个围墙,围墙的一部分就是 凸包长度,另一部分 是一个 以 l 为半径的 圆!!
代码:
题意:给 n 个点,你见一个围墙,使所有的点到围墙的距离不小于 l 。求围墙长度。。
思路:先求出n个点的 凸包,然后根据凸包建一个围墙,围墙的一部分就是 凸包长度,另一部分 是一个 以 l 为半径的 圆!!
代码:
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; int m,top; struct point { int x,y; }P[2000],S[2000],T; void find_pole_point() //找到基点 交换到 0 { int i,j=0; T=P[0]; for(i=1;i<m;i++) { if(P[i].y<T.y||(P[i].y==T.y&&P[i].x<T.x)) { j=i; T=P[i]; } } T=P[0]; P[0]=P[j]; P[j]=T; } double dis(point t1,point t2) //计算两点距离 { double z=(t1.x-t2.x)*(t1.x-t2.x)+(t1.y-t2.y)*(t1.y-t2.y); return sqrt(z); } double cross(point t1,point t2,point t3,point t4) //向量叉积 { return (t2.x-t1.x)*(t4.y-t3.y)-(t2.y-t1.y)*(t4.x-t3.x); } bool cmp(point t1,point t2) { double z= cross(P[0],t1,P[0],t2); return z? z>0:dis(P[0],t1)>dis(P[0],t2); } void scanner() //求凸包 { int i,j; find_pole_point(); sort(P+1,P+m,cmp); S[0]=P[0]; S[1]=P[1]; top=1; for(i=2;i<m;i++) { while(cross(S[top-1],S[top],S[top],P[i])<0) top--; top++; S[top]=P[i]; } } int main() { int i,j,ncase,l; while(scanf("%d%d",&m,&l)!=EOF) { for(i=0;i<m;i++) scanf("%d%d",&P[i].x,&P[i].y); scanner(); double ans= 2*3.1415926*l; for(i=0;i<=top;i++) ans+= dis(S[i],S[(i+1)%(top+1)]); printf("%.0lf\n",ans); } }
相关文章推荐
- POJ1113---Wall(基础计算几何:凸包入门)
- Wall(凸包POJ 1113)
- poj 1113 Wall(裸凸包)
- POJ 1113 Wall (凸包)
- POJ 1113 Wall 包凸入门题
- POJ 1113 Wall (计算几何-凸包)
- POJ 1113 || HDU 1348: wall(凸包问题)
- POJ 1113-Wall(凸包-Graham算法)
- poj 1113 Wall 计算几何 凸包 Graham扫描
- POJ 1113 WALL (凸包问题)
- Wall(凸包POJ 1113)
- POJ 1113 Wall(Graham求凸包周长)
- POJ - 1113 Wall(计算几何凸包)
- POJ1113:Wall(凸包)
- POJ1113 wall 简单凸包
- poj_1113 Wall(凸包)
- POJ1113(Wall) && Hdu1392(Surround the Trees) (计算几何--凸包)
- POJ 1113 Wall 二维凸包
- Wall - POJ 1113(求凸包)
- nyoj 226 && hdu HDU 1348 && poj 1113 wall [凸包周长]