poj2187 凸包问题
来源:互联网 发布:5230手机加密软件 编辑:程序博客网 时间:2024/05/23 15:41
Pseudo-Code: Graham Scan Algorithm
Input: a set of pointsS = {P = (P.x,P.y)}
Select the rightmost lowest point P0 in S.
Sort S angularly about P0 as a center.
For ties, discard the closer points.
Let P[N] be the sorted array of points.
Push P[0]=P0 and P[1] onto a stack W.
while i < N
{
Let PT1 = the top point on W
Let PT2 = the second top point on W
if (P[i] is strictly left of the line PT2 to PT1) {
Push P[i] onto W
i++ // increment i
}
else
Pop the top point PT1 off the stack
}
Output: W = the convex hull ofS.
转自:凸包问题的几种算法,包括Graham scan 算法
这个算法其实也很好想通的。
第一步:找到y坐标最小的点p0;
第二步:以p0为原点,对所有的点按逆时针顺序排序;
第三步:逐步迭代,加入边界点。(画出一条边,如果一个新的边更靠左,则加入,否则不加入)。
#include<iostream>#include<fstream>#include<algorithm>using namespace std;const int num=50005;int N;struct Point{int x;int y;}points[num],output[num];inline int ccw(Point p1,Point p2,Point p3) //交叉乘积{return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);}inline int dis(Point p1,Point p2) //距离{return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);}int cmp(const void *po1,const void *po2) //排序用的比较函数{Point *p1=(Point *)po1;Point *p2=(Point *)po2;int temp=ccw(points[0],*p1,*p2); //先判断方向if(temp>0){return -1;}else if(temp<0){return 1;}else{return dis(*p1,points[0])-dis(*p2,points[0]); //共线时按距离从大到校排序}}int maxDist( ){int min_index=0;for(int i=1;i<N;i++){if(points[i].x<points[min_index].x)min_index=i;else if(points[i].x<points[min_index].x&&points[i].y<points[min_index].y)min_index=i;}swap(points[0],points[min_index]); //找到y坐标最小的点,并交换到第一个位置qsort(points+1,N-1,sizeof(Point),cmp); //排序/*for(int i=0;i<N;i++){printf("%d %d\n",points[i].x,points[i].y);}*/output[0]=points[0];output[1]=points[1];int top=1;int i=2;while(i<N){//printf("%d %d\n",points[i].x,points[i].y);int temp=ccw(output[top-1],output[top],points[i]);if(temp>=0){output[++top]=points[i];i++;}/*else if(temp==0){output[top]=points[i];i++;}*/else{top--;}}int maxD=0;for(int i=0;i<=top;i++) //计算边界点之间的距离{//printf("%d %d\n",output[i].x,output[i].y);for(int j=i+1;j<=top;j++){int temp=dis(output[i],output[j]);if(temp>maxD)maxD=temp;}}printf("%d\n",maxD);return maxD;}int main(){freopen("input.txt","r",stdin);while(scanf("%d",&N)!=EOF) //输入{for(int i=0;i<N;i++){scanf("%d %d",&points[i].x,&points[i].y);//printf("%d %d\n",points[i].x,points[i].y);}maxDist();}return 0;}
- poj2187 凸包问题
- poj2187 凸包
- poj2187凸包
- POJ2187凸包
- POJ2187->凸包
- poj2187(凸包)
- poj2187(凸包)
- poj2187(凸包)
- POJ2187--凸包--Beauty Contest
- poj2187 凸包直径模板
- poj2187 Beauty Contest 凸包
- POJ2187 Beauty Contest 凸包+旋转卡壳
- poj2187 Beauty Contest 凸包+旋转卡壳
- POJ2187 Andrew凸包+旋转卡壳
- poj2187 Beauty Contest 凸包 Graham扫描
- 【POJ2187】Beauty Contest-凸包+旋转卡壳
- POJ2187-Beauty Contest- 最远点对问题(凸包+旋转卡壳)
- Poj2187(最远点对距离)凸包+旋转卡壳
- PHP操作PDF文档
- NOIP 2011 Day2 Problem3 (调试中)
- WinCE开机密码的输入方法
- 深入分析 Java 中的中文编码问题
- 新添资讯分类
- poj2187 凸包问题
- android 设置透明效果
- mysqlcheck
- 黑马程序员-基本数据类型
- NOIP 2011 Day1 Problem3
- [USACO 1.2.1] Milking Cows
- 数组查找
- 数组排序
- WinCE6.0 KITL概要