hdu6055(计算几何)计算n个整数点中有多少个正方形

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6055

题意:
有n个整数点,问这些点能组成多少个正多边形。加以推导,我们可以知道,只有是正方形时,才能使得所有的端点为整数点。

思路:
枚举正方形的一条边,另一条边可以根据这条边推导求得。
已知a(x1, y1), b(x2, y2), 则
c(x1+(y2-y1), y1-(x2-x1))
d(x2+(y2-y1), y2-(x2-x1))
求出线段ab对应的其中一条边cd(还有另一条边)

代码:

#include <iostream>
#include <algorithm>

using namespace std;
const int maxn = 600;

struct Point
{
    int x, y;
    Point(){}
    Point(int x, int y):x(x), y(y){}
    bool operator<(const Point &rhs)const{
        if(this->x<rhs.x){
            return true;
        }   
        else if(this->x==rhs.x){
            return this->y > rhs.y;
        }
        else{
            return false;
        }
    }
}A[maxn];

int main(){
    int n;
    while(cin>>n){
        for(int i=0; i<n; ++i){
            cin>>A[i].x>>A[i].y;
        }
        sort(A, A+n);
        int cnt = 0;

        for(int i=0; i<n-1; ++i){
            for(int j=i+1; j<n; ++j){

                int x1 = A[i].x+(A[j].y-A[i].y); int y1 = A[i].y-(A[j].x-A[i].x);
                int x2 = A[j].x+(A[j].y-A[i].y); int y2 = A[j].y-(A[j].x-A[i].x);
                Point a(x1, y1), b(x2, y2);
                int pos1 = lower_bound(A, A+n, a) - A;
                int pos2 = lower_bound(A, A+n, b) - A;
                    if(A[pos1].x==a.x&&A[pos1].y==a.y && A[pos2].x==b.x && A[pos2].y==b.y){
                        ++cnt;
                    }

            }
        }
        cout<<cnt/2<<endl;
    }   
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值