UVA 11626 Convex Hull(凸包:模板题)
2014-10-14 19:26
477 查看
UVA 11626 Convex Hull(凸包:模板题)
题意:
给你n个点的集合,并且用’N’或’Y’表示出来了该点是否在凸包的边界上,然后要你按逆时针顺序输出凸包且第一个输出的点必须是x坐标最小的(如果x坐标相同,那么就输出y坐标最小的).
分析:
其实本题就是一个求凸包的最大点集,然后输出的题目. 凸包的最大点集包含了所有在凸包边界上的点,所以输出点中有可能相邻3点共线.
刘汝佳<<训练指南>>P272页的模板(需要自己排序且把<=号改成<号)正好符合上面的几乎所有的要求,特别是最大点集,点逆时针输出,从x坐标最小的点开始输出…
AC代码:
题意:
给你n个点的集合,并且用’N’或’Y’表示出来了该点是否在凸包的边界上,然后要你按逆时针顺序输出凸包且第一个输出的点必须是x坐标最小的(如果x坐标相同,那么就输出y坐标最小的).
分析:
其实本题就是一个求凸包的最大点集,然后输出的题目. 凸包的最大点集包含了所有在凸包边界上的点,所以输出点中有可能相邻3点共线.
刘汝佳<<训练指南>>P272页的模板(需要自己排序且把<=号改成<号)正好符合上面的几乎所有的要求,特别是最大点集,点逆时针输出,从x坐标最小的点开始输出…
AC代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const double eps=1e-10; int dcmp(double x) { if(fabs(x)<eps) return 0; return x<0?-1:1; } struct Point { double x,y; Point(){} Point(double x,double y):x(x),y(y){} bool operator==(const Point& rhs)const { return dcmp(x-rhs.x)==0 && dcmp(y-rhs.y)==0; } bool operator<(const Point& rhs)const { return dcmp(x-rhs.x)<0 || (dcmp(x-rhs.x)==0 && dcmp(y-rhs.y)<0); } }; typedef Point Vector; Vector operator-(Point A,Point B) { return Vector(A.x-B.x,A.y-B.y); } double Cross(Vector A,Vector B) { return A.x*B.y-A.y*B.x; } int ConvexHull(Point *p,int n,Point *ch)//求凸包 { sort(p,p+n); n=unique(p,p+n)-p; int m=0; for(int i=0;i<n;i++) { while(m>1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2])<0) m--; ch[m++]=p[i]; } int k=m; for(int i=n-2;i>=0;i--) { while(m>k && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2])<0) m--; ch[m++]=p[i]; } if(n>1) m--; return m; } const int maxn=100000+10; Point p[maxn],ch[maxn]; int main() { int T; scanf("%d",&T); while(T--) { int n; scanf("%d",&n); for(int i=0;i<n;i++) scanf("%lf%lf %*c",&p[i].x,&p[i].y); int m=ConvexHull(p,n,ch); printf("%d\n",m); for(int i=0;i<m;i++) printf("%.0lf %.0lf\n",ch[i].x,ch[i].y); } return 0; }
相关文章推荐
- hdu 3662 3D Convex Hull (三维凸包模板)
- uva 10652 凸包 + 更新版计算几何模板
- 凸包 模板【uva10652】Board Wrapping
- UVA - 11168 Airport (凸包+整理模板)
- UVA 11626 凸包(含共线)
- UVA 10652 - Board Wrapping(凸包模板题)
- UVA 10652 - Board Wrapping 几何问题之凸包(附凸包模板)
- 3D Convex Hull (三维凸包模板)
- hdu 3662 3D Convex Hull (三维凸包 模板1)
- UVA 10078 || The Art Gallery (凸包:当前边与前一条边向×积符号一致
- hdu 2108 Shape of HDU(凸包模板题)
- uva11168 凸包
- UVa 10002 Center of Masses (凸包重心)
- uva10256 凸包
- uva10652 Board Wrapping(凸包)
- UVA 10173 最小矩形覆盖(凸包+旋转卡壳)
- UVA 11769 All Souls Night 的三维凸包要求的表面面积
- 【分解爪UVA11396-二分图染色模板】
- [USACO5.1]圈奶牛Fencing the Cows(凸包模板)
- UVA 811 - The Fortified Forest(凸包)