Snowflake Snow Snowflakes(哈希表的应用)

Snowflake Snow Snowflakes
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 27312   Accepted: 7213

题目链接:http://poj.org/problem?id=3349

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.

Source

 
题目大意:给出n片雪花,没片雪花有六个角,每个角的长度均给出,问有没有两片相同的雪花,如果存在,就输出“Twin snowflakes found.”,如果不存在,就输出“No two snowflakes are alike.”
思路:这道题目用链表来做比较简单,所以用类似于邻接表的思想。哈希表存储的是六角边之和对max的余数,作为同类雪花(六角边之和对max的余数相同的数)的头结点。
代码:
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #define max 100010
 5 struct vode
 6 {
 7     int f[6];
 8     struct vode *next;
 9 };
10 struct vode *hash[1000010];
11 int n,flag=0;
12 void add(int f[],int sum)
13 {
14     sum=sum%max;
15     int i,j;
16     struct vode *p;
17     p=hash[sum];
18     while(p)
19     {
20         for(i=0;i<=5;i++)
21         if(f[i]==p->f[0])
22         {
23             for(j=1;j<=5;j++)
24             if(f[(i+j)%6]!=p->f[j])break;
25             if(j==6)
26             {
27                 flag=1;
28                 return ;
29             }
30             for(j=1;j<=5;j++)
31             if(f[((i-j)%6+6)%6]!=p->f[j])break;
32             if(j==6)
33             {
34                 flag=1;
35                 return ;
36             }
37         }
38         p=p->next;
39     }
40     p=(struct vode *)malloc(sizeof(struct vode));
41     for(i=0;i<=5;i++)
42     p->f[i]=f[i];
43     p->next=hash[sum];
44     hash[sum]=p;
45 }
46 int main()
47 {
48     memset(hash,NULL,sizeof(hash));
49     scanf("%d",&n);
50     while(n--)
51     {
52         int i,f[6],sum=0;
53         for(i=0;i<=5;i++)
54         {
55             scanf("%d",&f[i]);
56             sum+=f[i];
57         }
58         if(flag)continue;
59         else add(f,sum);
60     }
61     if(flag)printf("Twin snowflakes found.");
62     else printf("No two snowflakes are alike.");
63     printf("\n");
64     return 0;
65 }
View Code

 

posted @ 2013-08-19 21:41  狂盗一枝梅  阅读(482)  评论(0编辑  收藏  举报