微信能否做门户网站,网站专业性免费评价工具,前端程序员需要掌握哪些基本技术,中国企业500强公司题意#xff1a; 判断凸包是否稳定。 解法#xff1a; 稳定凸包每条边上至少有三个点。 这题就在于求凸包的细节了#xff0c;求凸包有两种算法#xff1a; 1.基于水平序的Andrew算法 2.基于极角序的Graham算法 两种算法都有一个类似下面的语句#xff1a; for(int i0;i 判断凸包是否稳定。 解法 稳定凸包每条边上至少有三个点。 这题就在于求凸包的细节了求凸包有两种算法 1.基于水平序的Andrew算法 2.基于极角序的Graham算法 两种算法都有一个类似下面的语句 for(int i0;in;i) {while(m 1 Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) 0) m--;ch[m] p[i];} 这样的话求出来就是最简凸包即点数尽量少的凸包因为Cross 0的情况也被出栈了所以一条凸包边上就会三点共线了。 我们把语句改下把Cross.. 0 改成 Cross.. 0 那么求的就是最繁凸包即可能一条凸包边上包含很多点也属于凸包的点。 即下面的情况 最简凸包即为蓝色的四个点。 最繁凸包求出的是所有蓝点和红点。 作为这个题我们怎么求其实都可以 1.如果求最简凸包我们只需判断总共有多少个点在该凸包边上即可端点也算如果 3 则不符。 2.如果求的是最繁的凸包就不能用上面的判法因为怎么判都只有两个点了这时候可以采用下面的方法 假设要判断的边i那么判断边i和边i-1边i和边i1的夹角是否都为0180。 ----XDruid 代码 这里我用的是Andrew算法 #include iostream
#include cstdio
#include cstring
#include cstdlib
#include cmath
#include algorithm
#define eps 1e-8
using namespace std;struct Point{double x,y;Point(double x0, double y0):x(x),y(y) {}void input() { scanf(%lf%lf,x,y); }
};
typedef Point Vector;
int dcmp(double x) {if(x -eps) return -1;if(x eps) return 1;return 0;
}
template class T T sqr(T x) { return x * x;}
Vector operator (Vector A, Vector B) { return Vector(A.x B.x, A.y B.y); }
Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
bool operator (const Point a, const Point b) { return a.x b.x || (a.x b.x a.y b.y); }
bool operator (const Point a, const Point b) { return a.x b.x a.y b.y; }
bool operator (const Point a, const Point b) { return a.x b.x a.y b.y; }
bool operator (const Point a, const Point b) { return dcmp(a.x-b.x) 0 dcmp(a.y-b.y) 0; }
double Dot(Vector A, Vector B) { return A.x*B.x A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
double angle(Vector v) { return atan2(v.y, v.x); }bool OnSegment(Point P, Point A, Point B) { //端点不算return dcmp(Cross(A-P,B-P)) 0 dcmp(Dot(A-P,B-P)) 0;
}
int ConvexHull(Point* p, int n, Point* ch) {sort(p,pn);int m 0;for(int i0;in;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 in-2;i0;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;
}
Point ch[1006],p[1006];int main()
{int t,n,i,j;scanf(%d,t);while(t--){scanf(%d,n);for(i0;in;i) p[i].input();if(n 5) { puts(NO); continue; }int m ConvexHull(p,n,ch);if(m 2) { puts(NO); continue; }for(i0;im;i) {int cnt 0;for(j0;jn;j)if(OnSegment(p[j],ch[i],ch[(i1)%m]))cnt;if(cnt 3) break;}if(i m) puts(YES);else puts(NO);}return 0;
} View Code 现在终于对自己的凸包版有了全面的了解了妈妈再也不用担心我用错凸包了。哈哈。 转载于:https://www.cnblogs.com/whatbeg/p/4174944.html