hdu 3902 Swordsman

http://acm.hdu.edu.cn/showproblem.php?pid=3902

题意:给出一个简单多边形,判断这个多边形是否是轴对称多边形.

把原来的n个点扩展,加上各边的中点,扩展为2n个点。

然后对称轴一定是一个点和它对面的一个点组成。(i,i+n)

再验证在这条对称边的两端的对应两点组成的线段是否垂直平分这条对称轴.

理论上的复杂度是 O(n*n),但实际上应该达不到的吧=!

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

const int maxn = 20005;
const double eps = 1e-8;
struct node
{
	double x,y;
}a[2*maxn];
int n;

bool Can(int s,int e,int st,int et)
{
     if( fabs((a[s].x-a[e].x)*(a[st].x-a[et].x)+(a[s].y-a[e].y)*(a[st].y-a[et].y))>eps)
		 return false;
	 double x=(a[st].x+a[et].x)/2;
	 double y=(a[st].y+a[et].y)/2;
	 if( fabs((x-a[s].x)*(a[e].y-a[s].y)- (a[e].x-a[s].x )*(y-a[s].y) )>eps)
		 return false;
	 return true;
}

bool Is_true(int s,int e)
{
    for(int i=1;i<=n-1;i++)
	{
		int st = s-i;
		if(st<=0)
			st=st+2*n;
		int et = s+i;

		if( Can(s,e,st,et)==false)
			return false;
	}
	return true;
}

int main()
{
   while(scanf("%d",&n)!=EOF)
   {
      for(int i=1;i<=2*n;i+=2)
	      scanf("%lf %lf",&a[i].x,&a[i].y);
	  a[2*n+1]=a[1];
      for(int i=2;i<=2*n;i+=2)
	  {
		  a[i].x=(a[i-1].x+a[i+1].x)/2;
		  a[i].y=(a[i-1].y+a[i+1].y)/2;
	  }
	  int ok=0;
	  for(int i=1;i<=n;i++)
	  {
          if(Is_true(i,i+n)==true)
		  {
             ok=1;
			 break;
		  }	  
	  }
	  if(ok==1)
		  printf("YES\n");
	  else
		  printf("NO\n");
   }
   return 0;
}


下面是另一个版本的代码,复杂度是O(nlogn)

http://hi.baidu.com/nplusnplusnplu/blog/item/d260baef2e9e9c5879f055cb.html

 转化为字符串处理的。

 修改后的版本=

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

const int maxn=20010;
int N,M,next[maxn];
__int64 x[maxn],y[maxn];
__int64 ans;
struct Point 
{
	__int64 ed,ag;
}P[2*maxn],Q[maxn];

bool operator == (Point A,Point B)
{
	return A.ed==B.ed && A.ag==B.ag;
}

void Findnext ()
{
	next[0]=-1;
	next[1]=0;
	for (int i=2 ; i0;
}

inline __int64 sqr(__int64 x)
{
	return x*x;
}
void Getinf()
{
	for (int i=0 ; i

转载于:https://www.cnblogs.com/lwbaptx/archive/2011/08/04/2128008.html

你可能感兴趣的:(hdu 3902 Swordsman)