座標範圍很大,不能開二維數組,用vector也不方便搜索(不過應該也能做,畢竟數據不多)
定義hash函數爲x,y的平方和(別忘取mod)
運用一些數學原理,二重循環找正方形對角頂點座標,由此判斷另外兩個頂點的座標,並在hash表中尋找,效率非常高。
另外尋找的對角頂點不定,所以最後答案除以4。
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct po{
int x,y;
}a[1001];
int heade[2001],nexte[2001],px[2001],py[2001];
int t,ans,cnt;
void chushihua()
{
ans=0,cnt=0;
memset(heade,0,sizeof(heade));
}
void inhash(int x,int y)
{
int k=(x*x+y*y)%2001;
px[++cnt]=x;py[cnt]=y;
nexte[cnt]=heade[k];
heade[k]=cnt;
}
int fihash(int x,int y)
{
int k=(x*x+y*y)%2001;
int n=heade[k];
while(n!=0)
{
if(px[n]==x&&py[n]==y) return 1;
else n=nexte[n];
}
return 0;
}
int main()
{
cin>>t;
while(t!=0)
{
chushihua();
for(int i=1;i<=t;++i)
{
cin>>a[i].x>>a[i].y;
inhash(a[i].x,a[i].y);
}
for(int i=1;i<=t;++i)
for(int j=i+1;j<=t;++j)
{
int x1=a[i].x-(a[i].y-a[j].y);
int y1=a[i].y+(a[i].x-a[j].x);
int x2=a[j].x-(a[i].y-a[j].y);
int y2=a[j].y+(a[i].x-a[j].x);
if(fihash(x1,y1)&&fihash(x2,y2))
ans++;
}
for(int i=1;i<=t;++i)
for(int j=i+1;j<=t;++j)
{
int x1=a[i].x+(a[i].y-a[j].y);
int y1=a[i].y-(a[i].x-a[j].x);
int x2=a[j].x+(a[i].y-a[j].y);
int y2=a[j].y-(a[i].x-a[j].x);
if(fihash(x1,y1)&&fihash(x2,y2))
ans++;
}
ans >>= 2;
cout<<ans<<endl;
cin>>t;
}
return 0;
}