poj 3349 Snowflake Snow Snowflakes 数字哈希

数字哈希。我用的是雪花六个角之和取模。模很大 999983,哈希表是指针数组,链地址法处理冲突。

要考虑雪花顺时针和逆时针。

//============================================================================
//
// >     File        :    poj3349.cpp
// >     Author      :    flowertree
// >     Time        :    2015年11月12日
// >     Algorithm   :    数字哈希
//
//============================================================================

#include <iostream>
#include <string.h>
#include <algorithm>
#include <cstdio>
using namespace std;

#define Prime 999983
#define MAX 100005

struct snow
{
	__int64 edge[6];
	snow * next;
};

struct hashnode
{
	snow * next;
	hashnode()
	{
		next = NULL;
	}
};

snow node[MAX];
hashnode hashtable[Prime];
int node_count = 0;

snow * getnode()
{
	return &node[node_count++];
}

bool Judge(snow * a, snow * b)
{
	int i, j;
	int temp;
	bool mark;
	//顺时针
	for(i = 0; i < 6; i++)
	{
		mark = true;
		for(j = 0; j < 6; j++)
		{
			temp = (i + j) % 6;
			if(a -> edge[temp] != b -> edge[j])
				mark = false;
		}
		if(mark)
			break;
	}
	if(mark)
		return true;
	//逆时针
	for(i = 0; i < 6; i++)
	{
		mark = true;
		for(j = 0; j < 6; j++)
		{
			temp = (i - j + 6) % 6;
			if(a -> edge[temp] != b -> edge[j])
				mark = false;
		}
		if(mark)
			break;
	}
	if(mark)
		return true;
	else
		return false;
}

int main()
{
	int m;
	__int64 e[6];
	__int64 hashnum;
	snow * temp, * p;
	bool flag = false;
	cin >> m;
	while(m--)
	{
		hashnum = 0;
		temp = getnode();
		for(int i = 0; i < 6; i++)
		{
			scanf("%I64d", &e[i]);
			hashnum += e[i] % Prime;
			temp -> edge[i] = e[i];
		}
		if(flag)
			continue;
		hashnum = hashnum % Prime;
		if(hashtable[hashnum].next == NULL)
		{
			hashtable[hashnum].next = temp;
			temp -> next = NULL;
		}
		else
		{
			for(p = hashtable[hashnum].next; p -> next != NULL;	 p = p -> next)
			{
				flag = Judge(temp, p);
				if(flag)
					break;
			}
			if(!flag)
				flag = Judge(temp, p);
			if(!flag)
			{
				p -> next = temp;
				temp -> next = NULL;
			}
		}
	}
	if(flag)
	{
		cout << "Twin snowflakes found." << endl;
	}
	else
	{
		cout << "No two snowflakes are alike." << endl;
	}
	system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值