题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5533
Dancing Stars on Me
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 601 Accepted Submission(s):
320
were cold in a black sky. What a wonderful night. You observed that, sometimes
the stars can form a regular polygon in the sky if we connect them properly. You
want to record these moments by your smart camera. Of course, you cannot stay
awake all night for capturing. So you decide to write a program running on the
smart camera to check whether the stars can form a regular polygon and capture
these moments automatically.
Formally, a regular polygon is a convex
polygon whose angles are all equal and all its sides have the same length. The
area of a regular polygon must be nonzero. We say the stars can form a regular
polygon if they are exactly the vertices of some regular polygon. To simplify
the problem, we project the sky to a two-dimensional plane here, and you just
need to check whether the stars can form a regular polygon in this plane.
indicating the total number of test cases. Each test case begins with an
integer n
, denoting the number of stars in the sky. Following n
lines, each contains 2
integers xi
,y
i
, describe the coordinates of n
stars.
1≤T≤300
3≤n≤100
−10000≤xi
,y
i
≤10000
All coordinates are distinct.
can form a regular polygon. Otherwise, output "`NO`" (both without
quotes).
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
#define MAX 10010
#define INF 0x3f3f3f
#define DD double
using namespace std;
DD f(DD x1,DD y1,DD x2,DD y2)
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int main()
{
int t,n,m,j,i,k;
DD x[MAX],y[MAX];
DD s[MAX];
int vis[MAX];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%lf%lf",&x[i],&y[i]);
int k=0;
memset(vis,0,sizeof(vis));
DD Min;
int next=1;
int ans=1;
for(i=1;i<=n;i++)
{
Min=INF;
for(j=1;j<=n;j++)
{
if(next==j) continue;
//如果自己到自己就跳过
else if(!vis[j])
{
if(Min>f(x[next],y[next],x[j],y[j]))
{
Min=f(x[next],y[next],x[j],y[j]);
//找距离next点最近的点
ans=j;
}
}
}
next=ans; //找到下一个点
vis[next]=1;
s[k++]=Min;
}
int flag=1;
for(i=0;i<k-1;i++)
{
if(s[i]!=s[i+1])
{
flag=0;
break;
}
}
if(s[0]!=s[k-1])
flag=0;
if(flag)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}