POJ 2002 Squares

给你1000个点,让你在这些点中,找到4个点,使其成为一个正方形的顶点。问有多少对这种点。

选两个点,看剩下的两个点是否是给出来的,用hash过之,我用的map,差不多。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
map<int,int> mp;
long long cnt;
struct point{
    int x;
    int y;
};
point p[2000];

bool cmp(point a, point b)
{
    if(a.x == b.x) return a.y < b.y;
    return a.x < b.x;
}
int main()
{
    int i,t1,t2,n,tx,ty,t3,t4,j;
    while (1)
    {
        scanf("%d",&n);
        if (n == 0)
            break;
        cnt=0;
        mp.clear();
        for (i=0; i<n; i++)
        {
            scanf("%d%d",&p[i].x,&p[i].y);
            mp[p[i].x*100000+p[i].y]=1;
        }
        sort(p,p+n,cmp);
        for (i=0; i<n; i++)
        {
            for (j=i+1; j<n; j++)
            {
                t1=p[i].x+p[i].y-p[j].y;
                t2=p[i].y-p[i].x+p[j].x;
                t1=100000*t1+t2;
                t3=p[j].x+p[i].y-p[j].y;
                t4=p[j].y-p[i].x+p[j].x;
                t3=t3*100000+t4;
                if (mp.find(t1) != mp.end() && mp.find(t3) != mp.end())
                    cnt++;
            }
        }
        printf("%lld\n",cnt/2);
    }
}


你可能感兴趣的:(POJ 2002 Squares)