您的位置:首页 > 其它

poj2002 -- 点的hash

2014-03-08 19:48 190 查看
题目:

Squares

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

大意:给出n个点的坐标 求能组成多少个正方形。

思路:
依次枚举两个点,求出另外点的坐标,看看另外能组成正方形的点是否存在。

已知两点A(x1,y1)B(x2,y2)
则 另外两点的坐标:
x3=x1+|y1-y2|;
y3=y1-|x1-x2|;
x4=x2+|y1-y2|;
y2=y2-|x1-x2|;

最难的部分便是对点的储存了 ,

这里先给出 点储存的模板,通过hash数组和next数组的转换 达到储存点的目的:

code:

struct point
{
int x;
int y;
}poin[maxn];

int hash[maxn+10];
int next[maxn+10];

int findkey(point p)
{
return abs(p.x+p.y)%maxn;
}

void hashinsert(int i)
{
int key=findkey(poin[i]);
next[i]=hash[key];//最后一个next储存的是-1,其他的都指向下一个next
hash[key]=i;//指向最开头的next的下标
}

int hashsearch(point p)
{
int key=findkey(p);
int i=hash[key];
while(i!=-1)
{
if(p.x==poin[i].x&&p.y==poin[i].y)//找到一个这样的点
return 1;
i=next[i];
}
return 0;
}


题目代码:

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;

const int maxn=1031;

struct point
{
int x;
int y;
}poin[maxn];

int hash[maxn+10];
int next[maxn+10];

int findkey(point p)
{
return abs(p.x+p.y)%maxn;
}

void hashinsert(int i)
{
int key=findkey(poin[i]);
next[i]=hash[key];
hash[key]=i;
}

int hashsearch(point p)
{
int key=findkey(p);
int i=hash[key];
while(i!=-1)
{
if(p.x==poin[i].x&&p.y==poin[i].y)
return 1;
i=next[i];
}
return 0;
}

bool cmp(point p1,point p2)
{
if(p1.x!=p2.x)
return p1.x<p2.x;
else
return p1.y<p2.y;
}

int main()
{
int n;
int x1,x2,x3,x4;
int y1,y2,y3,y4;
//freopen("aa.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
memset(hash,-1,sizeof(hash));
memset(next,-1,sizeof(next));
point p3,p4;
for(int i=0;i<n;i++)
scanf("%d%d",&poin[i].x,&poin[i].y);
sort(poin,poin+n,cmp);
for(int i=0;i<n;i++)
hashinsert(i);
int ans=0;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
x1=poin[i].x;
y1=poin[i].y;
x2=poin[j].x;
y2=poin[j].y;
p3.x=x1+(y2-y1);
p3.y=y1-(x2-x1);
p4.x=x2+(y2-y1);
p4.y=y2-(x2-x1);
if(hashsearch(p3)&&hashsearch(p4))
ans++;
}
}
printf("%d\n",ans/2);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: