您的位置:首页 > 编程语言 > Go语言

poj 3349 Snowflake Snow Snowflakes

2015-07-21 09:51 861 查看
题目链接:http://poj.org/problem?id=3349

题       意:给你几组输入(六个数代表雪花的六个角),判断是否有相同的雪花。

思       路:使用哈希查找的方法将数据存入数组,若储存时有冲突发生,则已找到相同的雪花,后面的数据就都不需要处理。

                  注   :存储时,我使用的是数组的存储方式,并且将数组循环处理了。

                 原因:比如输入的是1 2 3 4 5 6,则2 3 4 5 6 1,3 4  5 6 1 2,……,6 5 4 3 2 1,5 4 3 2 1 6等都是相同形状的。

代码如下:

#include <iostream>
using namespace std;
#include <string.h>
#include <stdio.h>
#include <queue>
#include <algorithm>
#define M 1200010
#define m 1200007
typedef long long LL;
struct node
{
int num[6];
LL next;
};
node nu[M];
LL hashx[m], cur;
int gethash(int *num1)
{
int has = 0;
for( int i = 0; i < 6; i ++ )
has += num1[i];
return has%m;
}
int bijiao( int *num1, int *num2 )
{
for( int i = 0; i < 6; i ++ )
if( num1[i] != num2[i] )
return 0;
return 1;
}
void inserthash( int *num1, int h )
{
for( int i = 0; i < 6; i ++ )
nu[cur].num[i] = num1[i];
nu[cur].next = hashx[h];
hashx[h] = cur;
cur++;
}
int searchhash( int *num1 )
{
int h = gethash(num1);
int next = hashx[h];
while( next != -1 )
{
if( bijiao( num1, nu[next].num ) ) return 1;
next = nu[next].next;
}
inserthash(num1,h);
return 0;
}
int main()
{
int flag = 0, num1[2][12];
LL n ;
cur = 0;
scanf ( "%lld", &n );
for( int i = 0; i < m; i ++ )
hashx[i]=-1;
while( n-- )
{
for( int i = 0; i <6; i ++ )
{
scanf ( "%d", &num1[0][i] );
num1[0][i+6]=num1[0][i];
}
if( flag == 1 ) continue;
for( int i = 0; i < 6; i ++ )
num1[1][i+6] = num1[1][i] = num1[0][5-i];
for( int i = 0; i < 6; i ++ )
{
if( searchhash(num1[0]+i) || searchhash( num1[1]+i) )
{
flag = 1;
break;
}
}
}
if( flag ) printf("Twin snowflakes found.\n");
else printf("No two snowflakes are alike.\n");
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数字哈希 algorithm