2015 ICPC亚洲区域赛长春站 G Dancing Stars on Me(几何+暴力)

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 nlines, 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

题意:

给出n个点,判断这n个点能否组成正n边形。

思路:

由于n的范围较小,所以可以直接暴力求解。因为正n边形上各点到其他点的最短距离即是该正n边形的边长w,所以只要判断下所有点之间的最短距离是否均为w即可!

题目链接

#include<bits/stdc++.h>
#define MAX 100
#define INF 0x3f3f3f3f
using namespace std;
int x[MAX+5],y[MAX+5],e[MAX+5][MAX+5];
int dis(int a,int b)
{
    return ((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]));
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(e,INF,sizeof(e));
        int n,i,j,sum=0;
        scanf("%d",&n);
        for(i=1;i<=n;i++)
            scanf("%d%d",&x[i],&y[i]);
        int minn=INF;
        for(i=1;i<=n;i++)
        {
            for(j=i+1;j<=n;j++)
            {
                e[i][j]=dis(i,j);
                minn=min(minn,e[i][j]);
            }
        }
        for(i=1;i<=n;i++)
            for(j=i+1;j<=n;j++)
                if(minn==e[i][j])
                    sum++;
        if(sum==n) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

 

posted @ 2018-10-29 21:12  真想不出名字了  阅读(243)  评论(0编辑  收藏  举报