POJ 2002 二分 计算几何

根据正方形对角的两顶点求另外两个顶点公式:

x2 = (x1+x3-y3+y1)/2; y2 = (x3-x1+y1+y3)/2;

x4= (x1+x3+y3-y1)/2; y4 = (-x3+x1+y1+y3)/2;

#include<cstdio>  
#include<cstring>  
#include<algorithm>  
using namespace std;  
const int maxn=1000+5;  
struct Node  
{  
    int x,y;  
    bool operator<(const Node& rhs)const  
    {  
        return x<rhs.x || (x==rhs.x && y<rhs.y);  
    }  
}nodes[maxn];  
  
int main()  
{  
    int n;  
    while(scanf("%d",&n)==1 && n)  
    {  
        int ans=0;//正方形个数  
        for(int i=0;i<n;++i)  
            scanf("%d%d",&nodes[i].x,&nodes[i].y);  
        sort(nodes,nodes+n);  
  
        for(int i=0;i<n;++i)  
        for(int j=i+1;j<n;++j)  
        {  
            Node tmp;//tmp作为正方形的第3或4个点  
            tmp.x=nodes[i].x+nodes[i].y-nodes[j].y;  
            tmp.y=nodes[i].y+nodes[j].x-nodes[i].x;  
            if(!binary_search(nodes,nodes+n,tmp)) continue;  
            tmp.x=nodes[j].x+nodes[i].y-nodes[j].y;  
            tmp.y=nodes[j].y+nodes[j].x-nodes[i].x;  
            if(!binary_search(nodes,nodes+n,tmp)) continue;  
            ++ans;  
        }  
        printf("%d\n",ans/2);
    }  
    return 0;  
} 

 

posted @ 2017-09-10 20:51  Pacify  阅读(181)  评论(0编辑  收藏  举报