Squares
Time Limit:3500MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit
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
/*
哈希表的构建
知道两点可计算出另外两点的坐标
已知: (x1,y1) (x2,y2)
则: x3=x1+(y1-y2) y3= y1-(x1-x2)
x4=x2+(y1-y2) y4= y2-(x1-x2)
或
x3=x1-(y1-y2) y3= y1+(x1-x2)
x4=x2-(y1-y2) y4= y2+(x1-x2)
枚举两个点 之后查找另外两个点 由于有重复 所以最后除以4
*/
#include<iostream>
#include<cstring>
#include<algorithm>
#include<functional>
using namespace std;
const int MAX = 29989;
int N, X[1005], Y[1005], cnt, head[MAX + 5];
struct HashNode
{
int val, next, x, y;
};
HashNode F[MAX];
void InitHash()
{
cnt = 0;
memset(head, -1, sizeof(head));
}
void InsertHash(int val, int x, int y)
{
int pos = (val < 0 ? -val : val) % MAX;
F[cnt].val = val, F[cnt].next = head[pos], F[cnt].x = x, F[cnt].y = y;
head[pos] = cnt++;
}
bool FindHash(int val, int x, int y)
{
int pos = (val < 0 ? -val : val) % MAX;
for (int i = head[pos]; i != -1; i = F[i].next)
if (x == F[i].x&&y == F[i].y)
return true;
return false;
}
int main()
{
cin.sync_with_stdio(false);
while (cin >> N && N)
{
InitHash();
for (int i = 1; i <= N; i++)
{
cin >> X[i] >> Y[i];
InsertHash((X[i] * X[i] + Y[i] * Y[i]) % MAX,X[i],Y[i]);
}
int Ans = 0;
for (int i = 1; i <= N - 1; i++)
for (int j = i + 1; j <= N; j++)
{
int a = X[j] - X[i];
int b = Y[j] - Y[i];
int x3 = X[i] + b;
int y3 = Y[i] - a;
int x4 = X[j] + b;
int y4 = Y[j] - a;
if (FindHash((x3*x3 + y3*y3) % MAX,x3,y3) && FindHash((x4*x4 + y4*y4) % MAX,x4,y4))
{
Ans++;
//printf("第%d个正方形 : (%d,%d) (%d,%d) (%d,%d) (%d,%d)\n", Ans, X[i], Y[i], X[j], Y[j], x3, y3, x4, y4);
}
x3 = X[i] - b;
y3 = Y[i] + a;
x4 = X[j] - b;
y4 = Y[j] + a;
if (FindHash((x3*x3 + y3*y3) % MAX, x3, y3) && FindHash((x4*x4 + y4*y4) % MAX, x4, y4))
{
Ans++;
//printf("第%d个正方形 : (%d,%d) (%d,%d) (%d,%d) (%d,%d)\n", Ans, X[i], Y[i], X[j], Y[j], x3, y3, x4, y4);
}
}
cout << Ans / 4 << endl;
}
return 0;
}