POJ3349 Language: Snowflake Snow Snowflakes

POJ3349 Language: Snowflake Snow Snowflakes

题目:传送门 

 


 

 

题解:

   链表+hash的一道水题

   填个坑补个漏...

    


 

 

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7 typedef long long LL;
 8 int n,tot,mod=99991;
 9 int a[10],snow[110000][10],head[110000],next[110000];
10 int Hash(int *a) {
11     int sum=0,ans=1;
12     for(int i=0;i<6;i++) {
13         sum=(sum+a[i])%mod;
14         ans=(LL)ans*a[i]%mod;
15     }
16     return (sum+ans)%mod;
17 }
18 bool Find(int *a,int *b) {
19     for(int i=0;i<6;i++)
20         for(int j=0;j<6;j++) {
21             bool bk=true;
22             for(int k=0;k<6;k++)if(a[(i+k)%6]!=b[(j+k)%6])bk=false;
23             if(bk)return true;
24             bk=true;
25             for(int k=0;k<6;k++)if(a[(i+k)%6]!=b[(j-k)%6])bk=false;
26             if(bk)return true;
27         }
28     return false;
29 }
30 bool pd(int *a) {
31     int x=Hash(a);
32     for(int i=head[x];i;i=next[i])if(Find(snow[i],a))return true;
33     ++tot;
34     for(int i=0;i<6;i++)snow[tot][i]=a[i];
35     next[tot]=head[x];
36     head[x]=tot;
37     return false;
38 }
39 int main() {
40     scanf("%d",&n);tot=0;
41     for(int i=1;i<=n;i++) {
42         for(int j=0;j<6;j++)scanf("%d",&a[j]);
43         if(pd(a)) {
44             printf("Twin snowflakes found.");
45             return 0;
46         }
47     }
48     printf("No two snowflakes are alike.");
49     return 0;
50 }

 

posted @ 2018-04-18 09:22  CHerish_OI  阅读(174)  评论(0编辑  收藏  举报