Dancing Stars on Me
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 370 Accepted Submission(s): 222
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
题目大意:
就是判断给出的坐标是不是能够组成一个正 m 边形。。。
解题思路:
首先要知道,只有当 m == 4 的时候才有可能是正方形,别的都不可能(因为坐标是整数)。。。
所以只要判断是不是正方形就 ok 了, 可是给出的坐标不一定是按照顺序给出的,所以要判断一下,让他按照顺序给出。。。
上代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include <algorithm> #include <set> using namespace std; #define MM(a) memset(a,0,sizeof(a)) typedef long long LL; typedef unsigned long long ULL; const int maxn = 1e3+5; const int mod = 1000000007; const double eps = 1e-7; LL gcd(LL a, LL b) { if(b == 0) return a; return gcd(b, a%b); } struct node { int x, y; } arr[maxn]; int xx[maxn], yy[maxn]; bool cmp(node a, node b) { if(a.y != b.y) return a.x < b.x; return a.y < a.y; } int main() { int T, m; cin>>T; while(T--) { cin>>m; for(int i=0; i<m; i++) cin>>arr[i].x>>arr[i].y; if(m != 4) puts("NO"); else { sort(arr, arr+4, cmp); if(arr[1].y < arr[0].y) swap(arr[1], arr[2]); swap(arr[2], arr[3]); /*for(int i=0; i<4; i++) cout<<arr[i].x<<" "<<arr[i].y<<endl;*/ bool ok = false; for(int i=1; i<m; i++) { xx[i] = arr[i].x - arr[i-1].x; yy[i] = arr[i].y - arr[i-1].y; } xx[0] = arr[3].x - arr[0].x; yy[0] = arr[3].y - arr[0].y; int sum = 0; for(int i=1; i<m; i++) { if((xx[i]*xx[i]+yy[i]*yy[i]) == (xx[i-1]*xx[i-1]+yy[i-1]*yy[i-1])) sum++; } if(xx[0]*xx[1] + yy[0]*yy[1] == 0) ok = true; if(sum==3 && ok) puts("YES"); else puts("NO"); } } return 0; } /** 4 0 1 1 0 2 1 1 2 **/