HDU 5533 Dancing Stars on Me(凸包)

题目大意

  • 给出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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值