Squares
Time Limit: 3500MS | Memory Limit: 65536K | |
Total Submissions: 19942 | Accepted: 7674 |
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
Source
题目意思:
给出N个点的坐标,求用其中4个点能组成的正方形的个数,顺序不同计为重复。
解题思路:
hash表使用拉链法解决冲突。
依次枚举两个点,根据这两个点计算能够构成正方形的其他两个点的坐标。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXN 100007
#define INF 0x3f3f3f3f
struct se
{
int x,y;
} s[1010];
int ha[MAXN+5];//ha[i]表示键值i的下标
int next[MAXN+5];//next[i]表示对于当前位置下标i,如果ha中的键值重复,存储上一个该键值的下标;否则为0表示无重复
void Insert(int x,int y,int pos)//拉链法解决冲突
{
int t=(x*x+y*y)%MAXN,q=ha[t];
while(q)//已经存在
q=next[q];
next[pos]=ha[t];
ha[t]=pos;
}
bool Find(int x,int y)
{
int t=(x*x+y*y)%MAXN,q=ha[t];
while(q)//存在
{
if(s[q].x==x&&s[q].y==y) return true;
q=next[q];
}
return false;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("G:/cbx/read.txt","r",stdin);
//freopen("G:/cbx/out.txt","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n;
while(cin>>n&&n)
{
int ans=0;
memset(ha,0,sizeof(ha));
memset(next,0,sizeof(next));
for(int i=1; i<=n; ++i)
{
cin>>s[i].x>>s[i].y;
Insert(s[i].x,s[i].y,i);
}
for(int i=1; i<=n; ++i)
for(int j=i+1; j<=n; ++j)
{
se a,b;
//另外两个点在上方
a.x=s[i].x-(s[j].y-s[i].y);
a.y=s[i].y+(s[j].x-s[i].x);
b.x=a.x+(s[j].x-s[i].x);
b.y=a.y+(s[j].y-s[i].y);
if(Find(a.x,a.y)&&Find(b.x,b.y)) ++ans;
//两外两个点在下方
a.x=s[i].x+(s[j].y-s[i].y);
a.y=s[i].y-(s[j].x-s[i].x);
b.x=a.x+(s[j].x-s[i].x);
b.y=a.y+(s[j].y-s[i].y);
if(Find(a.x,a.y)&&Find(b.x,b.y)) ++ans;
}
cout<<ans/4<<endl;
}
return 0;
}
/*
1 1
1
5 3
4 5 3 2 1
*/