原题链接:​​http://poj.org/problem?id=2002​


一:原题内容

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


二:分析理解

在坐标系,给出n个点坐标,求能组成多少个正方形?数据保证没有相同的点坐标。

枚举两个点,求出另外正方形两点,看是否存在。

三:AC代码

#include<iostream>    
#include<string.h>
#include<algorithm>
#include<cmath>

#define N 1000005
using namespace std;

struct Node
{
int x;
int y;
};


bool cmp(Node node1, Node node2)
{
if (node1.x == node2.x)
return node1.y < node2.y;
return node1.x < node2.x;
}

Node node[N];
int head[N];
int next1[N];
int n;
int m;

void Insert(int i)
{
int key = (node[i].x*node[i].x + node[i].y*node[i].y) % N;
next1[m] = head[key];//这里之所以有m,因为下面的sort操作,改变了原始的node数组,所以下面注释的代码不能用
node[m].x = node[i].x;
node[m].y = node[i].y;
head[key] = m++;

//next1[i]=head[key];
//head[key]=i;
}

int Find(int x,int y)
{
int key = (x*x + y*y) % N;
for (int i = head[key]; i != -1; i = next1[i])
if (node[i].x == x&&node[i].y == y)
return i;
return -1;
}

int main()
{
while (scanf("%d", &n) && n)
{
memset(head, -1, sizeof(head));
memset(next1, -1, sizeof(next1));
m = 1005;
for (int i = 0; i < n; i++)
{
scanf("%d%d", &node[i].x, &node[i].y);
Insert(i);
}

sort(node, node + n, cmp);
int ans = 0;
for (int i = 0; i <= n - 2; i++)
{
for (int j = i + 1; j <= n - 1; j++)
{
int x1, y1;
int x2, y2;
int width1 = node[i].y - node[j].y;
int width2 = node[j].x - node[i].x;

if (width1 > 0)
{
x1 = node[i].x - width1;
y1 = node[i].y - width2;
if (Find(x1, y1) == -1)//没找到
continue;

x2 = node[j].x - width1;
y2 = node[j].y - width2;
if (Find(x2, y2) == -1)
continue;

ans++;
}
else
{
width1 = -width1;
x1 = node[j].x - width1;
y1 = node[j].y + width2;
if (Find(x1, y1) == -1)
continue;

x2 = node[i].x - width1;
y2 = node[i].y + width2;
if (Find(x2, y2) == -1)
continue;

ans++;
}
}
}

printf("%d\n", ans / 2);
}

return 0;
}