POJ 3349 Snowflake Snow Snowflakes Hash

时间:2023-03-08 19:41:43

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

 #include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
const int prime = ; struct Snow
{
int leg[];
}snow[]; struct HashTable
{
struct Snow *data;
struct HashTable *next;
HashTable()
{
next = NULL;
}
}; struct HashTable *Hash[prime]; bool clockwise(const struct Snow &a, const struct Snow &b)
{
for(int i = , j; i < ; i++)
{
if(a.leg[i] == b.leg[])
{
for(j = ; j < ; j++)
{
if(a.leg[(i+j)%] != b.leg[j])
break;
}
if(j >= )return ;
}
}
return ;
} bool counterclockwise(const struct Snow &a, const struct Snow &b)
{
for(int i = , j; i < ; i++)
{
if(a.leg[i] == b.leg[])
{
for(j = ; j < ; j++)
{
if(a.leg[(i+j)%] != b.leg[-j])
break;
}
if(j >= )return ;
}
}
return ;
} bool hash_insert(int key, struct Snow *snow_ptr)
{
if(Hash[key] == NULL)
{
Hash[key] = new HashTable;
Hash[key]->data = snow_ptr;
}
else
{
struct HashTable *p = Hash[key];
while(p != NULL)
{
if(clockwise(*p->data, *snow_ptr) || counterclockwise(*p->data, *snow_ptr))
return ;
else p = p->next;
}
p = new HashTable;
p->data = snow_ptr;
}
return ;
} int main()
{
int n, key;
while(scanf("%d", &n) != EOF)
{
bool ok = ;
memset(Hash, , sizeof(Hash));
for(int i = ; i < n; i++)
{
key = ;
for(int j = ; j < ; j++)
{
scanf("%d", &snow[i].leg[j]);
key += snow[i].leg[j];
}
key %= prime;
if(!ok)
{
ok = hash_insert(key, &snow[i]);
}
}
printf("%s\n", ok ? "Twin snowflakes found." : "No two snowflakes are alike.");
}
return ;
}