您的位置:首页 > 其它

POJ 2202 哈希

2015-11-30 23:15 302 查看
Squares

Time Limit: 3500MS Memory Limit: 65536K

Total Submissions: 18116 Accepted: 6946

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

题意:

给出坐标轴上的多个点,问最多这些点可以组成多少个正方形。

题解:

直接暴力枚举肯定是不行的。考虑只要知道了两个点,就能推出有可能的两个正方形。然后做一个hash表,查找这两个点是否在哈希表里。最后可知一个正方形四个边。最后的结果需要除以4。用数学推点的时候需要用到高中的数学知识。推导过程不算特别麻烦,这里直接给出结论。

已知两个点A(x1,y1),B(x2,y2).

则其中一个正方形的两点为:

C1(x2 + y1 - y2,y2 + x2 - x1)

D1(x1 + y1 - y2,y1 + x2 - x1)

另一个正方形的两点为:

C2(x2 + y2 - y1,y2 + x1 - x2)

D2(x1 + y2 - y1,y1 + x1 - x2)

hash键值k = (x^2+y^2)%M

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define M 10007

using namespace std;

struct Po{
int x,y;
Po* next;
};

Po G[M];
Po* Hash[M+5] = {NULL};

void init(void){
for(int i = 0;i<M+5;i++)
Hash[i] = NULL;
}

bool IsFind(Po tem)
{
int k = (tem.x*tem.x+tem.y*tem.y)%M;
if(Hash[k] == NULL)
return false;
else{
Po* p = Hash[k];
while(p!=NULL){
if(tem.x==p->x&&tem.y==p->y)
return true;
p = p->next;
}
}
return false;
}

int main()
{
//    freopen("data.txt","r",stdin);
int n;
while(scanf("%d",&n)&&n)
{
init();
int i,j;
for(i = 0;i<n;i++){
int a,b;
scanf("%d%d",&a,&b);
G[i].x = a;
G[i].y = b;
int k = (a*a+b*b)%M;
Po* tem = (Po*)malloc(sizeof(Po));
tem->next = NULL;
tem->x = a;
tem->y = b;
if(Hash[k]==NULL) Hash[k] = tem;
else
{
tem->next = Hash[k];
Hash[k] = tem;
}
}
long long sum = 0;
for(i = 1;i<n;i++)
for(j = 0;j<i;j++){
if(i!=j){
int x1 = G[i].x;
int y1 = G[i].y;
int x2 = G[j].x;
int y2 = G[j].y;
Po tem;
int ok1 = 0;
tem.x = x2+y1-y2;
tem.y = y2+x2-x1;
if(IsFind(tem)){
tem.x = x1+y1-y2;
tem.y = y1+x2-x1;
if(IsFind(tem))
ok1 = 1;
}
int ok2 = 0;
tem.x = x2+y2-y1;
tem.y = y2+x1-x2;
if(IsFind(tem)){
tem.x = x1+y2-y1;
tem.y = y1+x1-x2;
if(IsFind(tem))
ok2 = 1;
}
if(ok1) sum++;
if(ok2) sum++;
}
}

printf("%lld\n",sum/4);
}

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