Problem Description
The sky was brushed clean by the wind and the stars 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.
Input
The first line contains a integer T 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,yi, describe the coordinates of n stars.
1≤T≤300
3≤n≤100
−10000≤xi,yi≤10000
All coordinates are distinct.
Output
For each test case, please output “YES
” if the stars can form a regular polygon. Otherwise, output “NO
” (both without quotes).
Sample Input
3
3
0 0
1 1
1 0
4
0 0
0 1
1 0
1 1
5
0 0
0 1
0 2
2 2
2 0
Sample Output
NO
YES
NO
Source
2015ACM/ICPC亚洲区长春站-重现赛(感谢东北师大)
问给出的点是不是一个规则凸包,边一样长。
直接求一个凸包(包含共线的),判边长是否一样长。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
using namespace std;
#define MAX 505
#define INF 1000000000
int n,L;
struct point { //构造点
double x,y;
} p[MAX];
point save[505]; //存点
int xmult(point p1,point p2,point p0) {
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
bool cmp(const point& a,const point &b) {
if(a.y == b.y)return a.x < b.x;
return a.y < b.y;
}
int Graham(point *p,int n) {
int i;
sort(p,p + n,cmp);
save[0] = p[0];
save[1] = p[1];
int top = 1;
for(i = 2; i < n; i++) {
while(top && xmult(save[top],p[i],save[top-1])>0) top--;//去掉=就是包含共线。。
save[++top] = p[i];
}
int mid = top;
for(i = n - 2; i >= 0; i--) {
while(top>mid&&xmult(save[top],p[i],save[top-1])>0) top--;//去掉=就是包含共线。。
save[++top]=p[i];
}
return top;
}
double dis(point a,point b) {
double x=a.x-b.x;
double y=a.y-b.y;
return x*x+y*y;
}
int main() {
int t;
scanf("%d",&t);
while(t--) {
scanf("%d",&n);
int i,j;
for(i=0; i<n; i++) scanf("%lf %lf",&p[i].x,&p[i].y);
int tot=Graham(p,n);
if(tot!=n) {
printf("NO\n");
continue;
}
double s=dis(save[0],save[1]);
bool flag=true;
for(i=1; i<tot; i++) {
if(abs(s-dis(save[i],save[(i+1)%tot]))>0.0001) {
flag=false;
break;
}
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}