您的位置:首页 > 其它

HDU 5533 Dancing Stars on Me(凸包)

2015-11-04 16:30 369 查看

题目大意

给出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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: