POJ 2002 Squares

解题思路:

枚举两个顶点作为对角边,计算正方形另一对角边的两个顶点,查找这两个顶点是否都存在

此思路中每个正方形均会计算两次,所以最后结构需要/2

NULL
#include <iostream>
using namespace std;
#define PRIME 39997
#define MAXN 1000
int point[MAXN][2],first[PRIME],next[MAXN];
inline
int CalHash(int a[2])
{
int ans = a[0]*a[0]+a[1]*a[1];
ans
%=PRIME;
return ans;
}
inline
bool Find(int a[2])
{
int hash,i;
hash
= CalHash(a);
for(i=first[hash];i!=-1;i=next[i])
if(point[i][0]==a[0]&&point[i][1]==a[1])return true;
return false;
}
int main()
{
int i,j,n,hash,t,ans,a[2],b[2];
while(scanf("%d", &n) && n)
{
for(i=0;i<PRIME;i++)first[i]=-1;
for(i=0;i<MAXN;i++)next[i]=-1;
for (i=0;i<n;i++)
{
scanf(
"%d %d", &point[i][0], &point[i][1]);
hash
= CalHash(point[i]);
next[i]
=first[hash],first[hash]=i;
}
for (ans=0,i=1;i<n;i++)
{
for(j=0;j<i;j++)
{
t
=point[i][0]+point[j][0]-point[i][1]+point[j][1];
if(t%2)continue; a[0]=t/2;
t
=point[i][0]+point[j][0]+point[i][1]-point[j][1];b[0]=t/2;
t
=point[i][0]-point[j][0]+point[i][1]+point[j][1];a[1]=t/2;
t
=point[i][1]+point[j][1]-point[i][0]+point[j][0];b[1]=t/2;
if(Find(a)&&Find(b))ans++;
}
}
printf(
"%d\n",ans/2);
}
return 0;
}

 

posted on 2010-12-11 16:23  ltang  阅读(180)  评论(0编辑  收藏  举报

导航