首页 > 代码库 > poj 3349 -- Snowflake Snow Snowflakes
poj 3349 -- Snowflake Snow Snowflakes
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 30652 | Accepted: 8076 |
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.
思路:利用散列(hash)将雪花hash成较小范围的值,然后在这个值中查找。我用了拉链法实现hash。
1 /*====================================================================== 2 * Author : kevin 3 * Filename : SnowflakeSnowSnowflackes.cpp 4 * Creat time : 2014-07-23 17:17 5 * Description : 6 ========================================================================*/ 7 #include <iostream> 8 #include <algorithm> 9 #include <cstdio>10 #include <cstring>11 #include <queue>12 #include <cmath>13 #define clr(a,b) memset(a,b,sizeof(a))14 #define M 502115 using namespace std;16 struct Snow{17 int s[6];18 }snow;19 vector<Snow>vec[M+5];20 bool judge(int adr,int k,Snow sno)21 {22 int i,j;23 for(i = 0; i < 6; i++){24 if(sno.s[0] == vec[adr][k].s[i]){25 for(j = 1; j < 6; j++){26 if(sno.s[j] != vec[adr][k].s[(j+i)%6])27 break;28 }29 if(j == 6) return true;30 for(j = 1; j < 6; j++){31 if(sno.s[6-j] != vec[adr][k].s[(i+j)%6])32 break;33 }34 if(j == 6) return true;35 }36 }37 return false;38 }39 int SearchHash(int key,Snow sno)40 {41 int adr = key%M;42 int len = vec[adr].size();43 for(int i = 0; i < len; i++){44 if(judge(adr,i,sno)){45 return 1;46 }47 }48 vec[adr].push_back(sno);49 return 0;50 }51 int main(int argc,char *argv[])52 {53 int n,flag = 0;54 scanf("%d",&n);55 while(n--){56 int sum = 0;57 scanf("%d",&snow.s[0]);58 sum = snow.s[0];59 for(int i = 1; i < 6; i++){60 scanf("%d",&snow.s[i]);61 sum = sum^snow.s[i];62 }63 if(!flag){64 if(SearchHash(sum,snow)){ // 若找到65 flag = 1;66 }67 }68 }69 if(!flag) printf("No two snowflakes are alike.\n");70 else{71 printf("Twin snowflakes found.\n");72 }73 return 0;74 }