题目大意
给出n个点,判断这n个点能否构成一个等边的严格凸包
分析
首先直接求凸包,判断是不是n个点全部用到了。 再依次比较每条边是否相等。
代码
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std ;
const int maxn = 110 ;
struct point {
int x , y;
bool operator < (point const &rhs) const {
return (x < rhs.x || (x == rhs.x && y < rhs.y));
}
};
point p[maxn] , ch[maxn];
int cross(point const &O , point const &A , point const &B){
int xoa = A.x - O.x;
int yoa = A.y - O.y;
int xob = B.x - O.x;
int yob = B.y - O.y;
return xoa * yob - yoa * xob;
}
double dis(point A , point B)
{
return sqrt ((double )(A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));
}
int ConvexHull(point *p , int n , point *ch)
{
sort(p , p + n);
int m = 0 ;
for (int i = 0 ; i < n; i++) {
while (m > 1 && cross(p[i] , ch[m-1 ] , ch[m-2 ]) < 0 ) m--;
ch[m++] = p[i];
}
int k = m;
for (int i = n - 2 ; i >= 0 ; i--) {
while (m > k && cross(p[i] , ch[m-1 ] , ch[m-2 ]) < 0 ) m--;
ch[m++] = p[i];
}
if (n > 1 ) m--;
return m;
}
int main()
{
int t , n;
cin >> t;
while (t--)
{
cin >> n;
for (int i = 0 ; i < n; i++) cin >> p[i].x >> p[i].y;
int cnt = ConvexHull(p , n , ch);
if (cnt != n) cout << "NO" << endl;
else {
double d = dis(ch[0 ] , ch[1 ]);
int sign = 1 ;
for (int i = 2 ; i < n; i++) {
double tmp = dis(ch[i-1 ] , ch[i]);
if (tmp != d) {sign = 0 ; break ;}
}
double tmp = dis(ch[0 ] , ch[n-1 ]);
if (tmp != d) sign = 0 ;
if (sign) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
return 0 ;
}