凸包问题--Graham-Scan算法

凸包问题的两个算法,上一篇学习了简单好写的卷包裹法,现在我们学习Graham-Scan法。

首先复习一下中学学过的极坐标。

平面中取一定点A,从A点出发的一条射线AM,再选定一个长度单位和角度的正方向(通常取逆时针方向)。

对于平面内任意一点B,都可以用有序对(\rho\theta)(0<=\theta<2)唯一表示,\rho表示线段AB的长度,\theta表示从AM到AB的角度。

这样建立的坐标系称为极坐标系,定点A称为极点,射线AM称为极轴,\rho称为点B的极径,\theta称为点B的极角,有序对(\rho,\theta)就称点A的极坐标。

若极坐标系中定点A与直角坐标系的原点O重合,极坐标系中的极轴为直角坐标系X轴正半轴,于X轴逆时针成90°角为Y正半轴,则空间中任意一点A在极坐标系中坐标(\rho,\theta)与直角坐标系中的坐标(x,y)转化关系为:x=\rhocos\theta  y=\rhosin\theta

简单复习之后(毕竟中学已经学过了)开始学习Graham-Scan算法。

原理:

Grajam-Scan是一种灵活的凸包算法,其总时间复杂度仅为O(nlogn)。Graham扫描法的原理是从点集中先找出一个最左下方的点,可以证明,这个点肯定在凸包上(易证),然后以这个点为极点,将所有点根据与这个点的极角排序,并且同时使用一个栈结构维护凸包上的点。

按照极角序依次将点与栈顶的两个点拐向判断:若右拐,则将当前点加入栈中;否则,将栈顶的点弹出。当遍历完点集后,还在栈中的点就是凸包上的点,而且依次出栈可以得到从起点开始顺时针旋转的所有凸包上的点。

算法步骤:

Step1:选出x坐标最小的点作为极点(x坐标相同,选y最小的点)。这个点必在凸包上。

Step2:将其余点按极角排序,在极角相同的情况下比较与极点的距离,离极点比较近的优先。

Step3:用一个栈S存储凸包上的点,先将按极角和极点排序最小的两个点入栈。

Step4:按需扫描每个点,检查栈顶的前两个元素与这个点构成的折线段是否“拐”向右侧(叉积<=0)。

Step5:如果满足,则弹出栈顶元素,并返回Step4再次检查,直至不满足。将该点入栈,并对其他点不断执行Step5操作。

Step6:最终栈中的元素为凸包的顶点序列。

算法类似上一个章节的卷包裹法。样例流程也是一样的(我甚至代码都没有改)完全可以参照卷包裹法写出代码:

想开上一章卷包裹法||样例流程的点开这个链接:https://blog.csdn.net/qq_40482358/article/details/88064135

模板:输入n个点,输出这n个点所围成的凸包,从原点开始顺时针输出凸包的顶点。

同样两份代码,Graham-Scan扫描法和Jarvis步进法。

Code1-Graham-Scan扫描法:

//#pragma comment(linker, "/STACK:1024000000,1024000000")
  
#include<stdio.h>
#include<string.h> 
#include<math.h> 
   
#include<map>  
//#include<set>
#include<deque> 
#include<queue> 
#include<stack> 
#include<bitset>
#include<string> 
#include<fstream>
#include<iostream> 
#include<algorithm> 
using namespace std; 
  
#define ll long long 
#define Pair pair<int,int>
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b)
#define clean(a,b) memset(a,b,sizeof(a))// 水印
//std::ios::sync_with_stdio(false);
//  register
const int MAXN=1e3+10;
const int INF32=0x3f3f3f3f;
const ll INF64=0x3f3f3f3f3f3f3f3f;
const ll mod=1e9+7;
const double EPS=1.0e-8;
const double PI=acos(-1.0);

struct Point{
	double x,y;
	Point(double _x=0,double _y=0){
		x=_x;y=_y;
	}
	friend Point operator + (const Point &a,const Point &b){
		return Point(a.x+b.x,a.y+b.y);
	}
	friend Point operator - (const Point &a,const Point &b){
		return Point(a.x-b.x,a.y-b.y);
	}
	friend double operator ^ (Point a,Point b){//向量叉乘 
		return a.x*b.y-a.y*b.x;
	}
};
struct V{
	Point start,end;double ang;
	V(Point _start=Point(0,0),Point _end=Point(0,0),double _ang=0.0){
		start=_start;end=_end;ang=_ang;
	}
	friend V operator + (const V &a,const V &b){
		return V(a.start+b.start,a.end+b.end);
	}
	friend V operator - (const V &a,const V &b){
		return V(a.start-b.start,a.end-b.end);
	}
};
struct Circle{
	double r;
	Point centre;
	Circle(Point _centre=Point(0,0),double _r=0){
		centre=_centre;r=_r;
	}
};
Point Dots[MAXN];
Point stk[MAXN];int top;
int n;

double Distance(Point a,Point b){
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double Parellel(double key){
	return fabs(key)<EPS?0:key;
}
int Cmp(Point a,Point b){
	double res=Parellel((a-Dots[1])^(b-Dots[1]));
	if(res>0) return 1;
	if(res==0&&Distance(a,Dots[1])<Distance(b,Dots[1])) return 1;
	return 0;
}
void Graham(){
	sort(Dots+2,Dots+1+n,Cmp);
	top=2;stk[1]=Dots[1];stk[2]=Dots[2];
	for(int i=3;i<=n;++i){
		while(top>=2&&((stk[top]-stk[top-1])^(Dots[i]-stk[top-1]))<EPS) --top;
		stk[++top]=Dots[i];
	}stk[top+1]=stk[1];
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;++i){
		scanf("%lf%lf",&Dots[i].x,&Dots[i].y);
	}int k=1;
	for(int i=2;i<=n;++i){
		if(Dots[i].y<Dots[k].y||(Dots[i].y==Dots[k].y&&Dots[i].x<Dots[k].x)){
			k=i;
		}
	}swap(Dots[1],Dots[k]);
	Graham();
	double ans=0;
	for(int i=1;i<=top;++i){
		if(stk[i].x==0&&stk[i].y==0){
			for(int j=i;j<=top;++j){
				printf("(%lf,%lf)\n",Dots[j].x,Dots[j].y);
			}
			for(int j=1;j<i;++j){
				printf("(%lf,%lf)\n",Dots[j].x,Dots[j].y);
			}break;
		}
	}
}

/*
10
0 0
70 -50
60 30
-30 -50
80 20
50 -60
90 -20
-30 -40
-10 -60
90 10


*/

Code2这个是Jarvis步进法:

//#pragma comment(linker, "/STACK:1024000000,1024000000")
  
#include<stdio.h>
#include<string.h> 
#include<math.h> 
   
#include<map>  
#include<set>
#include<deque> 
#include<queue> 
#include<stack> 
#include<bitset>
#include<string> 
#include<fstream>
#include<iostream> 
#include<algorithm> 
using namespace std; 
  
#define ll long long 
#define Pair pair<int,int>//,pair<int,int> >
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b)
#define clean(a,b) memset(a,b,sizeof(a))// 水印
//std::ios::sync_with_stdio(false);
//  register
const int MAXN=3e2+10;
const int INF32=0x3f3f3f3f;
const ll INF64=0x3f3f3f3f3f3f3f3f;
const ll mod=1e9+7;
const double EPS=1.0e-8;
const double PI=acos(-1.0);

struct Point{
	double x,y;
	Point(double _x=0,double _y=0){
		x=_x;y=_y;
	}
	friend Point operator + (const Point &a,const Point &b){
		return Point(a.x+b.x,a.y+b.y);
	}
	friend Point operator - (const Point &a,const Point &b){
		return Point(a.x-b.x,a.y-b.y);
	}
	friend double operator ^ (Point a,Point b){//向量叉乘 
		return a.x*b.y-a.y*b.x;
	}
}p[MAXN];
struct V{
	Point start,end;double ang;
	V(Point _start=Point(0,0),Point _end=Point(0,0),double _ang=0.0){
		start=_start;end=_end;ang=_ang;
	}
	friend V operator + (const V &a,const V &b){
		return V(a.start+b.start,a.end+b.end);
	}
	friend V operator - (const V &a,const V &b){
		return V(a.start-b.start,a.end-b.end);
	}
};
int n,res[MAXN],top;

int Cmp(Point a,Point b){
	if(a.y==b.y)
		return a.x<b.x;
	return a.y<b.y;
}
int Mult(Point sp,Point ep,Point op){
	return (sp.x-op.x)*(ep.y-op.y)>=(ep.x-op.x)*(sp.y-op.y)-EPS;
}
void Jarvis(){
	int len;top=1;
	sort(p,p+n,Cmp);
	if(n==0) return ;res[0]=0;
	if(n==1) return ;res[1]=1;
	if(n==2) return ;res[2]=2;
	for(int i=2;i<n;++i){
		while(top&&Mult(p[i],p[res[top]],p[res[top-1]])) --top;
		res[++top]=i;
	}len=top;
//	cout<<"first for()"<<top<<endl;
	res[++top]=n-2;
	for(int i=n-3;i>=0;--i){
		while(top!=len&&Mult(p[i],p[res[top]],p[res[top-1]])) --top;
		res[++top]=i;
	}
//	cout<<"second for()"<<top<<endl;
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;++i){
		scanf("%lf%lf",&p[i].x,&p[i].y);
	}
	Jarvis();int s;//cout<<top<<endl;
	for(s=0;s<top;++s){
		if(!p[res[s]].x&&!p[res[s]].y) break;
	}
	for(int i=s;i<top;++i){
		printf("(%lf,%lf)\n",p[res[i]].x,p[res[i]].y);
	}
	for(int i=0;i<s;++i){
		printf("(%lf,%lf)\n",p[res[i]].x,p[res[i]].y);
	}
}
/*
10
0 0
70 -50
60 30
-30 -50
80 20
50 -60
90 -20
-30 -40
-10 -60
90 10

*/

下面是验证板子:

POJ-1873-The Fortified Forest,题解:https://blog.csdn.net/qq_40482358/article/details/88081113

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值