Dancing Stars on Me

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1282    Accepted Submission(s): 713


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





思路:

给你n个点,问你能不能用这n个点(全用上)构成一个正m边形。
一开始除了暴力啥思路都没有,但是慢慢的就发现,因为点都是整数点,所以除了正方形,其他的正m边形是构不成的。

所以,只需要对n==4的情形进行判断,若构成正四边形,则把四条边+两条对角线排序,最大的一定是相等的两条对角线并且和边不等,最小的四个肯定是四条边。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

struct rectangle
{
	int x;
	int y;
}a[4];

int main()
{
	int t;
	while(scanf("%d",&t)!=EOF)
	{
		while(t--)
		{
			int n;
			scanf("%d",&n);
			if(n!=4)
			{
				for(int i=0;i<n;i++)
				{
					int q;
					int w;
					cin>>q>>w;
				}
			}
			else 
			{
				for(int i=0;i<4;i++)
				{
					cin>>a[i].x>>a[i].y;
				}
				
			}
			if(n!=4)printf("NO\n");
			else 
			{
				int len[6];
				int k=0;
				for(int i=0;i<4;i++)
					for(int j=i+1;j<4;j++)
					{
						len[k++]=(a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y);
					}
				sort(len,len+6);
				if(len[0]==len[1]&&len[1]==len[2]&&len[2]==len[3]&&len[3]!=len[4]&&len[4]==len[5])
				{
					printf("YES\n");
				}
				else printf("NO\n");
			}	
		}
	}
	return 0;
}


思路:
给你n个点,问你能不能用这n个点(全用上)构成一个正m边形。
一开始除了暴力啥思路都没有,但是慢慢的就发现,因为点都是整数点,所以除了正方形,其他的正m边形是构不成的。

所以,只需要对n==4的情形进行判断,若构成正四边形,则把四条边+两条对角线排序,最大的一定是相等的两条对角线并且和边不等,最小的四个肯定是四条边。


代码: