题意:给你二维平面点集 让你找出其中能组成多少个正方形
思路:对每一个点进行哈希 枚举两个点做为正方形的一条边 然后再求另外两个点 看看原点集中是否存在这两个点 由于枚举的是每条边 则一个正方形会被枚举四次 结果要除以四
#include <cstdio>
#include <cstring>
using namespace std;
#define REP( i, a, b ) for( int i = a; i < b; i++ )
#define CLR( a, x ) memset( a, x, sizeof a )
const int maxn = 1000 + 10;
const int MOD = 1007;
struct Point{
int x, y;
Point(int x = 0, int y = 0) : x(x), y(y) {}
void read() { scanf("%d%d", &x, &y); }
int Hash(){ return (x * x + y * y) % MOD; }
}p[maxn];
struct HashTable{
int x, y;
HashTable *next;
HashTable(){ next = NULL; }
} *Hashtable[maxn];
int n;
void Insert(Point P){
int key = P.Hash();
if(!Hashtable[key]){
HashTable *tmp = new HashTable;
tmp -> x = P.x;
tmp -> y = P.y;
Hashtable[key] = tmp;
}
else{
HashTable *tmp = Hashtable[key];
while(tmp -> next) tmp = tmp -> next;
tmp -> next = new HashTable;
tmp -> next -> x = P.x;
tmp -> next -> y = P.y;
}
}
bool Find(Point P){
int key = P.Hash();
if(Hashtable[key]){
HashTable *tmp = Hashtable[key];
while(tmp){
if(tmp -> x == P.x && tmp -> y == P.y) return true;
tmp = tmp -> next;
}
}
return false;
}
void Creat(const Point &p1, const Point &p2, Point &p3, Point &p4, Point &p5, Point &p6){
int X = p2.x - p1.x;
int Y = p2.y - p1.y;
p3 = Point(p1.x + Y, p1.y - X);
p4 = Point(p2.x + Y, p2.y - X);
p5 = Point(p1.x - Y, p1.y + X);
p6 = Point(p2.x - Y, p2.y + X);
}
void solve(){
int ans = 0;
CLR(Hashtable, 0);
REP(i, 0, n) p[i].read(), Insert(p[i]);
REP(i, 0, n) REP(j, i+1, n){
Point p1, p2, p3, p4;
Creat(p[i], p[j], p1, p2, p3, p4);
if(Find(p1) && Find(p2)) ans++;
if(Find(p3) && Find(p4)) ans++;
}
printf("%d\n", ans / 4);
}
int main()
{
//freopen("in.txt", "r", stdin);
while(~scanf("%d", &n) && n) solve();
return 0;
}