/*
2002 - Squares
Time Limit:3500MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Status
Description
A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating about its centre by 90 degrees gives the same polygon. It is not the only polygon with the latter property, however, as a regular octagon also has this property.
So we all know what a square looks like, but can we find all possible squares that can be formed from a set of stars in a night sky? To make the problem easier, we will assume that the night sky is a 2-dimensional plane, and each star is specified by its x and y coordinates.
Input
The input consists of a number of test cases. Each test case starts with the integer n (1 <= n <= 1000) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (two integers) of each point. You may assume that the points are distinct and the magnitudes of the coordinates are less than 20000. The input is terminated when n = 0.
Output
For each test case, print on a line the number of squares one can form from the given stars.
Sample Input
4
1 0
0 1
1 1
0 0
9
0 0
1 0
2 0
0 2
1 2
2 2
0 1
1 1
2 1
4
-2 5
3 7
0 0
5 2
0
Sample Output
1
6
1
*****************************************
思路就是建立一个hash表储存输入的点,然后以链表的形式储存坐标和访问情况,遍历时只要搜索点的hash值就能找到点...所以能大幅减少时间复杂度
*/
#include<stdio.h>
#include<math.h>
#include<string.h>
const int H = 99991; //推荐大素数
const int MAX = 100001;
int sum;
struct point //链表用的不熟就借用了别人的,STL的LIST也不敢乱用
{
int x,y;
bool value;
point *next;
}dot[MAX]; //其实是HASH值的链表
int hash(int x,int y)
{
return (x*x+y*y)%H; //获取hash值
}
void Hash(point *p,int x, int y) //给hash值的链表赋值,将X,Y入链表
{
if(p->value == false)
{
p->x = x;
p->y = y;
p->value = true;
p->next = new point;
p->next->value = false;
}else
{
p=p->next;
Hash(p,x,y);
}
}
bool find(int x,int y) //验证点是否在hash表中出现过,也就是是否输入过
{
point *p = &dot[hash(x,y)];
while(p->value == true)
{
if((p->x == x) && (p->y == y))
{
return true;
}else
{
p = p->next;
}
}
return false;
}
int main()
{
// freopen("in.txt","r",stdin);
int n;
while(~scanf("%d",&n) && n!=0)
{
int dotx[MAX],doty[MAX];
int i,x,y;
for(i = 0 ; i < MAX ; i++ )
dot[i].value = false; //hash表为空
sum=0;
for(i = 0 ; i < n ; i++)
{
scanf("%d%d",&x,&y);
dotx[i] = x ;
doty[i] = y ;
Hash(&dot[hash(x,y)],x,y);
}
for(i = 0 ; i < n ; i ++)
{
for(int j = 0 ; j < i ; j++)
{
int a1 = dotx[i],b1 = doty[i];
int a2 = dotx[j],b2 = doty[j];
if(find(a1+(b2-b1),b1-(a2-a1)) == true && find(a2+(b2-b1),b2-(a2-a1)) == true){++sum;}//画个图就知道,已知一条边两点的坐标
if(find(a1-(b2-b1),b1+(a2-a1)) == true && find(a2-(b2-b1),b2+(a2-a1)) == true){++sum;}//另外两个点就很容易求到了
}
}
printf("%d\n",sum/4);
}
return 0;
}