首页 > 代码库 > poj 3349:Snowflake Snow Snowflakes(哈希查找,求和取余法+拉链法)
poj 3349:Snowflake Snow Snowflakes(哈希查找,求和取余法+拉链法)
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 30529 | Accepted: 8033 |
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
21 2 3 4 5 64 3 2 1 6 5
Sample Output
Twin snowflakes found.
Source
1 #include <iostream> 2 #include <stdio.h> 3 4 using namespace std; 5 6 #define MAXPRIME 100001 7 8 struct Hashnode{ 9 int len[6]; 10 Hashnode* next; 11 }; 12 Hashnode hashtable[MAXPRIME]={0}; //哈希表,拉链法解决哈希冲突 13 14 int getkey(Hashnode* p) //哈希函数,求和取余法。求一片雪花对应的key值 15 { 16 int i,key=0; 17 for(i=0;i<6;i++) 18 key = (key + p->len[i])%MAXPRIME; 19 return key; 20 } 21 22 bool clockwise(Hashnode* p,Hashnode* q) //顺时针看有没有相同的顺序 23 { 24 int i,j; 25 for(i=0;i<6;i++){ 26 for(j=0;j<6;j++) 27 if(p->len[j]!=q->len[(i+j)%6]) 28 break; 29 if(j>=6) 30 break; 31 } 32 //找到两片相同的雪花 33 if(i<6) 34 return true; 35 else 36 return false; 37 } 38 39 bool counterclockwise(Hashnode* p,Hashnode* q) //逆时针看有没有相同的顺序 40 { 41 int i,j; 42 for(i=0;i<6;i++){ 43 for(j=0;j<6;j++) 44 if(p->len[j]!=q->len[(i+6-j)%6]) 45 break; 46 if(j>=6) 47 break; 48 } 49 //找到两片相同的雪花 50 if(i<6) 51 return true; 52 else 53 return false; 54 } 55 56 bool iscom(Hashnode* p) //判断这片雪花是否和之前的有一片雪花完全相同 57 { 58 int key = getkey(p); 59 if(hashtable[key].next==NULL){ //没有冲突 60 hashtable[key].next = p; 61 return false; 62 } 63 else{ //产生冲突 64 //拉链法解决冲突 65 Hashnode* q = &hashtable[key]; 66 p->next = q->next; 67 q->next = p; 68 69 q = p->next; //从p的下一个开始比较 70 while(q){ 71 if(clockwise(p,q) || counterclockwise(p,q)) //顺时针或者逆时针看有一个方向有相同的顺序,说明找到了相同的雪花 72 return true; 73 q = q->next; 74 } 75 return false; 76 } 77 } 78 79 int main() 80 { 81 int n,i; 82 bool f = false; //找没找到两片相同的雪花,默认是没有 83 scanf("%d",&n); 84 while(n--){ 85 Hashnode* p = new Hashnode; 86 p->next = NULL; 87 for(i=0;i<6;i++) //读取这片雪花的6个花瓣的长度 88 scanf("%d",&p->len[i]); 89 if(iscom(p)){ //判断这片雪花是否和之前的有一片雪花相同,如果有,改变f的值。 90 //函数中顺便将这片花瓣的信息插入到哈希表。 91 f=true; 92 break; //没有输入完毕,中途退出也可以,省了300MS 93 } 94 } 95 //判断有没有找到两片一样的雪花 96 if(f) 97 printf("Twin snowflakes found.\n"); 98 else 99 printf("No two snowflakes are alike.\n");100 return 0;101 }
Freecode : www.cnblogs.com/yym2013