POJ 2002 Squares 哈希

时间:2023-03-09 05:37:15
POJ 2002 Squares  哈希

题目链接: http://poj.org/problem?id=2002

 #include <stdio.h>
#include <string.h> const int prime = ; struct Hash_table
{
int x, y;
struct Hash_table *next;
}*Hash[prime]; void Hash_insert(int x, int y)
{
int key = (x + y) % prime;
if(Hash[key] == NULL)
{
Hash[key] = new Hash_table;
Hash[key]->x = x;
Hash[key]->y = y;
Hash[key]->next = NULL;
return;
}
struct Hash_table *p = Hash[key]; //必须注意!下面几行一定是p->next,如果改成:
/*
while(p != NULL)
p = p->next;
p = new Hash_table;
p->x = x;
p->y = y;
p->next = NULL;
*/
//是绝对不行的,因为这里调试了半天了。。。。。。
//正解如下:
while(p->next != NULL)
p = p->next;
p->next = new Hash_table;
p->next->x = x;
p->next->y = y;
p->next->next = NULL;
} bool Hash_search(int x, int y)
{
int key = (x + y) % prime;
struct Hash_table *p = Hash[key];
while(p != NULL)
{
if(p->x == x && p->y == y)
return ;
p = p->next;
}
return ;
} int main()
{
int n, tx, ty, x[], y[];
int x3, x4, y3, y4;
while(scanf("%d", &n) != EOF && n)
{
memset(Hash, , sizeof(Hash));
for(int i = ; i < n; i++)
{
scanf("%d %d", &tx, &ty);
x[i] = tx + ;
y[i] = ty + ;
Hash_insert(x[i], y[i]);
}
int ans = ;
for(int i = ; i < n; i++)
{
for(int j = ; j < n; j++)
{
if(j == i)continue;
x3 = x[i]+(y[i]-y[j]);
y3 = y[i]-(x[i]-x[j]);
x4 = x[j]+(y[i]-y[j]);
y4 = y[j]-(x[i]-x[j]);
if(Hash_search(x3, y3) && Hash_search(x4, y4))
ans++;
}
}
printf("%d\n", ans/);
}
return ;
}