HDU3902 判断简单多边形是否对称

先求重心,然后枚举,对于奇数个顶点的多边形,枚举一个点和一条边的中点所成的直线,对于偶数个顶点的多边形,对称轴可能过两条边的中点或者两个顶点(O(n)枚举,由于要满足过重心这个条件,可以使该层复杂度优化为常数),然后判断即可(O(n))!!这题解题报告是用后缀数组来做~_~  Holy Shit

代码(各种模板堆积):

#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define EPS 1e-8
#define min(a,b) (a) + EPS < (b) ? (a) : (b)
#define max(a,b) (a) > (b) + EPS ? (a) : (b)
const int MAXN = 20010;
struct Point{
	double x,y;
	Point(){}
	Point(double _x,double _y):x(_x),y(_y){}
	void input(){
		scanf("%lf%lf",&x,&y);
	}
};
struct Line{
	double a,b,c;
};
Line l;

Point ps[MAXN];

inline double difcross(Point a,Point b,Point c){
	return (b.x-a.x)*(c.y-a.y) - (c.x-a.x)*(b.y-a.y);
}

inline double getarea(Point p[],int np){
	double area = 0;
	p[np] = p[0];
	for(int i = 0; i < np; ++i){
		area += p[i].x*p[i+1].y - p[i].y*p[i+1].x;
	}
	return area/2.0;
}

inline Point getgc(Point p[],int np){
	double area = getarea(p,np);
	Point tp;
	p[np] = p[0];
	tp.x = tp.y =0;
	for(int i = 0; i < np; ++i)
		tp.x += (p[i].x+p[i+1].x)*(p[i].x*p[i+1].y-p[i].y*p[i+1].x);
	for(int i = 0; i < np; ++i)
		tp.y += (p[i].y+p[i+1].y)*(p[i].x*p[i+1].y-p[i].y*p[i+1].x);
	tp.x /= (6.0*area);
	tp.y /= (6.0*area);
	return tp;
}

inline void getline(Point x,Point y,Line &l){
	l.a = y.y-x.y;
	l.b = x.x - y.x;
	l.c = y.x*x.y-x.x*y.y;
}

inline bool p2line(Point a,Line l){
	if(fabs(l.a*a.x+l.b*a.y+l.c) < EPS)return true;
	return false;
}
inline Point Mid(Point a,Point b){
	return Point((a.x+b.x)/2.0,(a.y+b.y)/2.0);
}
inline int abs(int a){
	return a > 0 ? a : -a;
}
inline int got(int i,int j,int n){
	if(j > 0)return (i+j)%n;
	else return got(i,n+j,n);
}

inline bool chuizhi(Line l,Line tl){
	if(fabs(l.a*tl.a+l.b*tl.b) < EPS)return true;
	return false;
}

int main(){
	int n;
	while (scanf("%d",&n) != EOF){
		for(int i = 0; i < n; ++i)ps[i].input();
		Point gc = getgc(ps,n);
		Line l,tl;
		bool duichen = 0;
		if(n&1){
			for(int i = 0; i < n; ++i){
				getline(ps[i],Mid(ps[got(i,n/2,n)],ps[got(i,-n/2,n)]),l);
				if(!p2line(gc,l))continue;
				bool ok = 1;
				for(int j = 1; j <= n/2; ++j){
					getline(ps[got(i,j,n)],ps[got(i,-j,n)],tl);
					if(!p2line(Mid(ps[got(i,j,n)],ps[got(i,-j,n)]),l) || !chuizhi(l,tl)){
						ok = 0;
						break;
					}
				}
				if(ok){
					duichen = 1;
					break;
				}
			}
		}
		else{
			for(int i = 0; i < n; ++i){
				getline(ps[i],ps[got(i,n/2,n)],l);
				if(!p2line(gc,l))continue;
				bool ok = 1;
				for(int j = 1; j <= n/2-1; ++j){
					getline(ps[got(i,j,n)],ps[got(i,-j,n)],tl);
					if(!p2line(Mid(ps[got(i,j,n)],ps[got(i,-j,n)]),l) || !chuizhi(l,tl)){
						ok = 0;
						break;
					}
				}
				if(ok){
					duichen = 1;
					goto abc;
				}
			}
			for(int i = 0; i < n; ++i){
				int s = i,t = got(i,1,n);
				getline(Mid(ps[s],ps[t]),Mid(ps[got(s,-(n/2-1),n)],ps[got(t,n/2-1,n)]),l);
				if(!p2line(gc,l))continue;
				bool ok = 1;
				for(int j =1; j <= n/2-1; ++j){
					getline(ps[got(s,-j,n)],ps[got(t,j,n)],tl);
					if(!p2line(Mid(ps[got(s,-j,n)],ps[got(t,j,n)]),l) || !chuizhi(l,tl)){
						ok = 0;
						break;
					}
				}
				if(ok){
					duichen = 1;
					break;
				}
			}
				
		}
abc:		if(duichen)puts("YES");
		else puts("NO");
		
	}
	return 0;
} 



你可能感兴趣的:(c,优化,struct,input)