HDU_5533_Dancing Stars on Me
Posted edward108
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU_5533_Dancing Stars on Me相关的知识,希望对你有一定的参考价值。
Dancing Stars on Me
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2002 Accepted Submission(s): 1168
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.
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.
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
- 给出n个二维坐标点判断能够构成正多边形
- 考虑正多边形顶点在同一个圆上
- 任意挑3个点找外心,得到一组圆心和半径
- 先对于每个点检测和圆心的距离
- 然后如果距离都相等那就判断每两个相邻点的距离也应该相等
- n的范围不大n^2复杂度判相邻即可
1 #include <iostream> 2 #include <string> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <climits> 7 #include <cmath> 8 #include <vector> 9 #include <queue> 10 #include <stack> 11 #include <set> 12 #include <map> 13 using namespace std; 14 typedef long long LL ; 15 typedef unsigned long long ULL ; 16 const int maxn = 1e2 + 10 ; 17 const int inf = 0x3f3f3f3f ; 18 const int npos = -1 ; 19 const int mod = 1e9 + 7 ; 20 const int mxx = 100 + 5 ; 21 const double eps = 1e-6 ; 22 const double PI = acos(-1.0) ; 23 24 struct node{ 25 double x, y; 26 node(double a=0LL, double b=0LL){ 27 x=a; y=b; 28 } 29 }; 30 node vex[maxn], center; 31 node CircumCenter(node a, node b, node c){ 32 double a1=b.x-a.x, b1=b.y-a.y, c1=(a1*a1+b1*b1)/2; 33 double a2=c.x-a.x, b2=c.y-a.y, c2=(a2*a2+b2*b2)/2; 34 double d=a1*b2-a2*b1; 35 return node(a.x+(c1*b2-c2*b1)/d,a.y+(a1*c2-a2*c1)/d); 36 } 37 double dis2(node a, node b){ 38 return pow(a.x-b.x,2)+pow(a.y-b.y,2); 39 } 40 bool oneLine(node a, node b, node c){ 41 return ((b.y-a.y)/(b.x-a.x))==((c.y-a.y)/(c.x-a.x)); 42 } 43 int T, n, ans; 44 double R, D[maxn][maxn]; 45 int main(){ 46 // freopen("in.txt","r",stdin); 47 // freopen("out.txt","w",stdout); 48 while(~scanf("%d",&T)){ 49 while(T--){ 50 ans=1; 51 scanf("%d",&n); 52 for(int i=1;i<=n;i++) 53 scanf("%lf %lf",&vex[i].x,&vex[i].y); 54 55 if(oneLine(vex[1],vex[2],vex[3])) 56 ans=0; 57 58 if(ans){ 59 center=CircumCenter(vex[1],vex[2],vex[3]); 60 R=dis2(center,vex[1]); 61 for(int i=1;i<=n;i++) 62 if(dis2(vex[i],center)!=R){ 63 ans=0; break; 64 } 65 } 66 67 if(ans){ 68 for(int i=1;i<=n;i++){ 69 D[i][i]=0.0; 70 for(int j=i+1;j<=n;j++) 71 D[i][j]=D[j][i]=dis2(vex[i],vex[j]); 72 sort(D[i]+1,D[i]+1+n); 73 } 74 for(int i=2;i<=n;i++) 75 if(!(D[i][2]==D[i][3] && D[i][2]==D[i-1][2])){ 76 ans=0; break; 77 } 78 } 79 puts(ans?"YES":"NO"); 80 } 81 } 82 return 0; 83 }
以上是关于HDU_5533_Dancing Stars on Me的主要内容,如果未能解决你的问题,请参考以下文章
HDU 5533 Dancing Stars on Me 计算几何瞎暴力
Dancing Stars on Me HDU - 5533