1. 程式人生 > >HDU_5533_Dancing Stars on Me

HDU_5533_Dancing Stars on Me

sea pro rec total cnblogs first mar 一個 msu

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.

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.

1T300
3n100
?10000xi,yi10000
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亞洲區長春站-重現賽(感謝東北師大)
  • 給出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