poj 3349(hash)

第一次用vector,,感觉蛮方便的。

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

2

3 1 2 4 5 6

3 2 1 4 5 6

View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <vector>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 
 9 #define MAXN 100010
10 #define HASH 100007
11 
12 vector<int> hash[HASH];//hash表,表中存储的是snow数组的下标
13 int snow[MAXN][6];
14 int n;
15 
16 bool is_same(int a,int b)//同构
17 {
18     for(int i=0;i<6;i++)
19     {
20         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])
21             return true;
22         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])
23             return true;
24     }
25     return false;
26 }
27 
28 int main()
29 {
30     while(~scanf("%d",&n))
31     {
32         for(int i=0;i<n;i++)
33             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]);
34         int sum,key,tag=0;
35         for(int i=0;i<n&& !tag;i++)
36         {
37             sum=0;
38             for(int j=0;j<6;j++)
39                 sum+=snow[i][j];
40             key=sum%HASH;
41             for(int j=0;j<hash[key].size();j++)
42             {
43                 if(is_same(i,hash[key][j]))
44                 {
45                     puts("Twin snowflakes found.");
46                     tag=1;
47                     break;
48                 }
49             }
50             hash[key].push_back(i);
51         }
52         if(!tag)
53             puts("No two snowflakes are alike.");
54     }
55     return 0;
56 }
posted @ 2012-10-26 19:37  Missa  阅读(304)  评论(0编辑  收藏  举报