poj 3349

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值