Squares
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.
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
题意: 现在给你一个坐标内的点集, 要求你判断出有多少个正方形, 四个点不同顺序组成的正方形算一个.
解题思路:
1. 枚举四个点, 判断是否能组成正方形, 太暴力了, 不可取.
2. 想法是枚举2个点, 判断另外两个点是否存在. (这样做会出现一个正方行重复4次计算)
推到另外两个点的坐标:
很容易得四个直角三角形全等,分别设两条直角边为L1,L2, L1',L2', L1'',L2'', L1''',L2'''
并且由全等定理有:L1 = L1‘ = L1’‘ = L1’‘’ , L2 = L2' = L2'' = L2''';
|L1| = y3-y1; |L1'| = x2-x1;
|L2| = x3-x1; |L2'| = y1-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);
3. 接下来就是用hash的开散列方法, 快速查找另外2个推到出来的点是否再点集里面.
我采用的方法是 value = (x*x+y*y) % PRIME;
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 1005
#define PRIME 99983
struct node
{
int x, y;
}pos[MAX];
int n;
int hash[PRIME], next[PRIME];
bool find(int x,int y)
{
for(int e = hash[(x*x+y*y)%PRIME]; e != -1; e = next[e])
{
if(pos[e].x == x && pos[e].y == y)
return true;
}
return false;
}
int main()
{
//freopen("input.txt","r",stdin);
while(scanf("%d",&n) != EOF)
{
if(n == 0) break;
memset(hash,-1,sizeof(hash));
for(int i = 0; i < n; ++i)
{
scanf("%d %d",&pos[i].x,&pos[i].y);
int value = pos[i].x*pos[i].x + pos[i].y*pos[i].y;
value %= PRIME;
next[i] = hash[value];
hash[value] = i;
}
int result = 0;
for(int i = 0; i < n-1; ++i)
{
for(int j = i+1; j < n; ++j)
{
int a = pos[i].x - pos[j].x;
int b = pos[i].y - pos[j].y;
int x3 = pos[i].x + b;
int y3 = pos[i].y - a;
int x4 = pos[j].x + b;
int y4 = pos[j].y - a;
if(find(x3,y3) && find(x4,y4))
result++;
x3 = pos[i].x - b;
y3 = pos[i].y + a;
x4 = pos[j].x - b;
y4 = pos[j].y + a;
if(find(x3,y3) && find(x4,y4))
result++;
}
}
printf("%d\n",result/4);
}
return 0;
}