当前位置: 代码迷 >> 综合 >> HDU 5533 Dancing Stars on Me(整数坐标能否构成正n变形)
  详细解决方案

HDU 5533 Dancing Stars on Me(整数坐标能否构成正n变形)

热度:34   发布时间:2023-12-08 11:04:04.0

题目链接:
HDU 5533 Dancing Stars on Me
题意:
给出n个二维坐标点且坐标都是整数,判断这n个整数点能否构成正n边形?
分析:
因为坐标是整数点,所以当且仅当n=4时才有可能构成正四边形(正方形)。
判断正方形的方法:(共6条边)

将这n个点的两两距离求出,从小到大排序,最短的四条边一定相等且是正方形的边长,而且要保证对角线也相等且是边长的根号2倍(去边长的平方的话就是2倍)。对角线是排序后的最长两条边。

以上均来源于网络!

//1428K 0MS
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAX_N=110;int T,n;
int x[MAX_N],y[MAX_N],edge[MAX_N*MAX_N];inline 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()
{//freopen("hdu5533in.txt","r",stdin);scanf("%d",&T);while(T--){scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d%d",&x[i],&y[i]);}if(n!=4){printf("NO\n");continue;}int total=0;for(int i=0;i<n;i++){for(int j=i+1;j<n;j++){edge[total++]=dis(i,j);}}sort(edge,edge+total);if(edge[0]==edge[1]&&edge[1]==edge[2]&&edge[2]==edge[3]&&edge[4]==edge[5]&&edge[4]==2*edge[3]){printf("YES\n");}else {printf("NO\n");}}return 0;
}