#include <iostream>
using namespace std;
const __int64 prime=999983;
class
{
public:
__int64 len[6];
}leaf[100001];
typedef class hashtable{
public:
__int64 len[6];
hashtable* next;
hashtable(){
next=NULL;
}
}hashtable;
hashtable *hash[prime+1];
__int64 compute_key(int k)
{
__int64 key=0;
for(int i=0;i<6;i++)
{
key+=leaf[k].len[i]%prime;
key%=prime;
}
return ++key;
}
bool clockwise(hashtable* p,int k)
{
for (int j=0;j<6;j++)
{
bool flag=true;
for(int i=0;i<6;i++)
{
if(leaf[k].len[i]!=p->len[(i+j)%6])
{
flag=false;
break;
}
}
if(flag)
return true;
}
return false;
}
bool counterclockwise(hashtable* p,int k)
{
for(int j=0;j<6;j++) //逆时针转动j格
{
bool flag=true;
for(int i=0;i<6;i++)
if(leaf[k].len[i] != p->len[(5-i-j+6)%6])
{
flag=false;
break;
}
if(flag)
return true;
}
return false;
}
bool insert(int k)
{
__int64 key=compute_key(k);
if(!hash[key])
{
hashtable* temp=new hashtable;
for(int i=0;i<6;i++)
{
temp->len[i]=leaf[k].len[i];
}
hash[key]=temp;
}
else
{
hashtable* temp=hash[key];
if(clockwise(temp,k)||counterclockwise(temp,k))
return true;
while(temp->next)
{
temp=temp->next;
if(clockwise(temp,k)||counterclockwise(temp,k))
return true;
}
temp->next=new hashtable;
for(int i=0;i<6;i++)
temp->next->len[i]=leaf[k].len[i];
}
return false;
}
int main()
{
int n;
while (cin>>n)
{
memset(hash,0,sizeof(hash));
bool flag=false;
for(int i=1;i<=n;i++)
{
for(int j=0;j<6;j++)
scanf("%I64d",&leaf[i].len[j]);
/*Hash*/
if(!flag) //当前还没有出现相同的雪花
flag=insert(i);
//若出现相同的雪花,则还需后续输入,但不再处理
}
if(flag)
cout<<"Twin snowflakes found."<<endl;
else
cout<<"No two snowflakes are alike."<<endl;
}
return 0;
}
poj 3349
最新推荐文章于 2016-08-08 21:08:09 发布