您的位置:首页 > 其它

POJ 2002 Squares [hash]

2014-12-12 17:23 435 查看
Squares

Time Limit: 3500MSMemory Limit: 65536K
Total Submissions: 16631Accepted: 6328
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

Source

Rocky Mountain 2004

题解:枚举两个点,算出另外两点坐标,看是否在给的点集里。

具体实现用hash,hash碰撞了就放在链表中,然后在链表里查找~(天猫所说的 pascal拉链哈希,,,

就写了个哈希函数然后对函数值指针拉链出来哈希)



#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<map>
#define ll long long
#define N 1005
#define mod 2007

using namespace std;

int n;
int x[2*N];
int y[2*N];
//map<pair<int,int>,int>mp;
int ans;
int head[2*N];
int next[2*N];
int m;

void insert(int i)
{
int key=(x[i]*x[i]+y[i]*y[i])%mod;
next[m]=head[key];
x[m]=x[i];
y[m]=y[i];
head[key]=m++;
}

void ini()
{
ans=0;
m=N;
memset(head,-1,sizeof(head));
//mp.clear();
int i;
for(i=1;i<=n;i++){
scanf("%d%d",&x[i],&y[i]);
insert(i);
//mp[ make_pair(x[i],y[i]) ]=i;
}
}

int find(int xx,int yy)
{
int key=(xx*xx+yy*yy)%mod;
int i;
for(i=head[key];i!=-1;i=next[i]){
if(x[i]==xx && y[i]==yy){
return 1;
}
}
return 0;
}

int ok(int i,int j)
{
int mx,my;
int x3,x4,y3,y4;
int he,cha;
mx=x[i]+x[j];
my=y[i]+y[j];
he=mx+my;cha=my-mx;
if(he%2!=0 || cha%2!=0) return 0;
he/=2;cha/=2;
x3=he-y[i];
// x3=mx+my-y[i];
y3=cha+x[i];
//   y3=my-(mx-x[i]);
if(find(x3,y3)==0) return 0;
x4=y[i]-cha;
// x4=mx-(my-y[i]);
// y4=my+(mx-x[i]);
y4=he-x[i];
if(find(x4,y4)==0) return 0;

return 1;
}

void solve()
{
int i,j;
for(i=1;i<n;i++){
for(j=i+1;j<=n;j++){
if(ok(i,j)==1){
ans++;
}
}
}
}

void out()
{
ans/=2;
printf("%d\n",ans);
}

int main()
{
//freopen("data.in","r",stdin);
// scanf("%d",&T);
//while(T--){
while(scanf("%d",&n)!=EOF){
if(n==0) break;
ini();
solve();
out();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: