您的位置:首页 > 其它

POJ 2002 Squares hash+链地址法处理冲突

2016-08-20 16:44 288 查看
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. 

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


    好久没有1A过题了,好感动...
    这个题的题目大意就是给你一系列的坐标,问你每次任意选四个点,所有的情况加起来,最多能组成多少正方形,其中相同的四个点组成的正方形为同一个。
    看到这个题,第一反应可能是四个for循环来强行找,但是只要估算一下复杂度就知道这种方法是不可行的。因为正方形有特别的性质,即已知两个点的坐标,另外两个点的坐标也是可以算出的(虽然会有两种)。因此,我们只需要遍历任意两个点,然后判断能与这两个点组成正方形的坐标的位置有没有点就可以了,因此,这里要用到哈希。
    这里给出我做题时的伪代码,最近我发现,做题时先写出伪代码,有助于理清思路,也有助于debug,是个好习惯。
///将所有的点hash存起来(以x与y的key值(x*x+y*y)%mod)
///枚举任意两点
///依次判断以这两点为边能否组成正方型 若能 则sum++

///判断能否组成正方形:
///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)

///因为此过程中每一个正方形被统计了4次 因此最后结果为sum/4
<pre name="code" class="cpp">#include <stdio.h>
#include <string.h>
#define Mod 2333
#define Key(x, y) (x*x+y*y)%Mod

struct node
{
int x, y;
struct node * Next;
}*a[Mod];

struct hehe
{
int x, y;
}Pos[1111];

void Creat()
{
for (int i = 0; i < Mod; i++)
{
a[i] = (struct node *) malloc (sizeof(struct node));
a[i]->Next = NULL;
}
}

void init()
{
for (int i = 0; i < Mod; i++)
a[i]->Next = NULL;
}

void Update(int x, int y)
{
struct node *p, *q;
p = (struct node *) malloc (sizeof(struct node));
p->x = x;
p->y = y;
p->Next = NULL;
q = a[Key(x, y)];
while (q->Next)
q = q->Next;
q->Next = p;
}

bool Search(int x, int y)
{
struct node *p;
p = a[Key(x, y)];
while (p->Next)
{
if (p->Next->x == x && p->Next->y == y)
return 1;
p = p->Next;
}
return 0;
}

int main()
{
int n, i, j;
Creat();
while (scanf("%d", &n), n)
{
init();
int sum = 0;
for (i = 0; i < n; i++)
{
scanf("%d%d", &Pos[i].x, &Pos[i].y);
Update(Pos[i].x, Pos[i].y);
}
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
int a = Pos[j].x - Pos[i].x;
int b = Pos[j].y - Pos[i].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(Search(x3,y3) && Search(x4,y4))
sum++;

x3 = Pos[i].x - b;
y3 = Pos[i].y + a;
x4 = Pos[j].x - b;
y4 = Pos[j].y + a;

if(Search(x3,y3) && Search(x4,y4))
sum++;
}
}
printf("%d\n", sum/4);
}
return 0;
}
</pre><pre name="code" class="cpp">///这个题最后的Memory为888KB,预示着我之后会发发发?


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