POJ 3349:Snowflake Snow Snowflakes(数的Hash)

Posted Shadowdsp

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 3349:Snowflake Snow Snowflakes(数的Hash)相关的知识,希望对你有一定的参考价值。

http://poj.org/problem?id=3349

Snowflake Snow Snowflakes
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 37609   Accepted: 9878

Description

You may have heard that no two snowflakes are alike. Your task is to write a program to determine whether this is really true. Your program will read information about a collection of snowflakes, and search for a pair that may be identical. Each snowflake has six arms. For each snowflake, your program will be provided with a measurement of the length of each of the six arms. Any pair of snowflakes which have the same lengths of corresponding arms should be flagged by your program as possibly identical.

Input

The first line of input will contain a single integer n, 0 < n ≤ 100000, the number of snowflakes to follow. This will be followed by n lines, each describing a snowflake. Each snowflake will be described by a line containing six integers (each integer is at least 0 and less than 10000000), the lengths of the arms of the snow ake. The lengths of the arms will be given in order around the snowflake (either clockwise or counterclockwise), but they may begin with any of the six arms. For example, the same snowflake could be described as 1 2 3 4 5 6 or 4 3 2 1 6 5.

Output

If all of the snowflakes are distinct, your program should print the message: No two snowflakes are alike. If there is a pair of possibly identical snow akes, your program should print the message: Twin snowflakes found.

Sample Input

2
1 2 3 4 5 6
4 3 2 1 6 5

Sample Output

Twin snowflakes found.
 1 #include<cstdio>
 2 #include<algorithm>
 3 #include <cstring>
 4 #include <iostream>
 5 using namespace std;
 6 #define MOD 14997
 7 struct node
 8 {
 9     int len[6];
10 }snow,f[15000][50];
11 int Hash[15000];
12 
13 /*
14 第一道Hash
15 判断两个雪花是否相同:
16 分别是顺时针旋转和逆时针旋转,只要有一次是满足条件的就可以当成是一样的
17 一开始MOD取了999983,结果MLE了,后来看了一下别人的才取了14997
18 */
19 bool check(node a,node b)
20 {
21     int i,j,k;
22     bool flag;
23     //顺时针旋转判断
24     for(i=0;i<6;i++){
25         flag=true;
26         for(j=i,k=0;k<6;k++,j=(j+1)%6){
27             if(a.len[j]!=b.len[k]){
28                  flag=false;break;
29             }
30         }
31         if(flag) return 1;
32     }
33     //逆时针旋转判断
34     for(i=0;i<6;i++){
35         flag=true;
36         for(j=i,k=0;k<6;k++,j=(j+5)%6){
37             if(a.len[j]!=b.len[k]){
38                  flag=false;break;
39             }
40         }
41         if(flag) return 1;
42     }
43     return 0;
44 }
45 
46 int main()
47 {
48     int n;
49     int now;
50     scanf("%d",&n);
51     //只有一组样例就不初始化了
52     bool flag=false;
53     for(int i=0;i<n;i++){
54         now=0;
55         for(int j=0;j<6;j++){
56             scanf("%d",&snow.len[j]);
57             now+=snow.len[j]%MOD;
58         }
59         if(flag) continue;
60         now%=MOD;
61         if(Hash[now]>0){
62             for(int j=1;j<=Hash[now];j++){
63                 if(check(snow,f[now][j])){
64                     flag=true;break;
65                 }
66             }
67         }
68         Hash[now]++;
69         f[now][Hash[now]]=snow;
70     }
71     if(flag) printf("Twin snowflakes found.\n");
72     else printf("No two snowflakes are alike.\n");
73     return 0;
74 }

2016-06-04

以上是关于POJ 3349:Snowflake Snow Snowflakes(数的Hash)的主要内容,如果未能解决你的问题,请参考以下文章

POJ3349 Snowflake Snow Snowflakes

POJ 3349 -- Snowflake Snow Snowflakes

POJ 3349 Snowflake Snow Snowflakes

poj3349 Snowflake Snow Snowflakes(HASH)

哈希—— POJ 3349 Snowflake Snow Snowflakes

POJ 3349:Snowflake Snow Snowflakes(数的Hash)