您的位置:首页 > 其它

poj 3349(hash)

2012-10-26 19:37 309 查看
第一次用vector,,感觉蛮方便的。

这个题需要判断同构,数据一样的枝条应该是不一定是相同的,但是数据貌似很水。比如

2

3 1 2 4 5 6

3 2 1 4 5 6

View Code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

#define MAXN 100010
#define HASH 100007

vector<int> hash[HASH];//hash表,表中存储的是snow数组的下标
int snow[MAXN][6];
int n;

bool is_same(int a,int b)//同构
{
for(int i=0;i<6;i++)
{
if(snow[a][0]==snow[b][i%6] && snow[a][1]==snow[b][(i+1)%6] && snow[a][2]==snow[b][(i+2)%6] && snow[a][3]==snow[b][(i+3)%6] && snow[a][4]==snow[b][(i+4)%6] && snow[a][5]==snow[b][(i+5)%6])
return true;
if(snow[a][5]==snow[b][i%6] && snow[a][4]==snow[b][(i+1)%6] && snow[a][3]==snow[b][(i+2)%6] && snow[a][2]==snow[b][(i+3)%6] && snow[a][1]==snow[b][(i+4)%6] && snow[a][0]==snow[b][(i+5)%6])
return true;
}
return false;
}

int main()
{
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
scanf("%d%d%d%d%d%d",&snow[i][0],&snow[i][1],&snow[i][2],&snow[i][3],&snow[i][4],&snow[i][5]);
int sum,key,tag=0;
for(int i=0;i<n&& !tag;i++)
{
sum=0;
for(int j=0;j<6;j++)
sum+=snow[i][j];
key=sum%HASH;
for(int j=0;j<hash[key].size();j++)
{
if(is_same(i,hash[key][j]))
{
puts("Twin snowflakes found.");
tag=1;
break;
}
}
hash[key].push_back(i);
}
if(!tag)
puts("No two snowflakes are alike.");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: