Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 20898 | Accepted: 5466 |
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.
题目连接:http://poj.org/problem?id=3349
算法类型:Hash
解题思路:这是一个简单的hash搜索,只要把雪花的6个臂的长度加起来再模一个大的质数,在储存在系统链表内,再分别调用一个比较函数.这个题目使用系统链表的调用,不过系统链表好像要比自己写链表运行慢一点,差不多多了800MS.....不过代码要比自己写链表少了不少。
算法实现:#include<stdio.h> #include<iostream> #include<vector> #include<string.h> #include<cstdio> #define MAX 9997 using namespace std; struct Snow { int point[6]; }snow[100009]; int Hash(Snow snow) { int add=0; int i; for(i=0;i<6;i++) { add=add+snow.point[i]; } return add%MAX; } int compare(Snow s1,Snow s2) { int flag; int begin[6]; int a; int cnt = 0; for(a=0; a<6; a++) { if(s1.point[0] == s2.point[a]) { begin[cnt++] = a; } } for(int s=0; s<cnt; s++) { int b; flag = 1; for(b=0; b<6; b++) {//顺时针 if(s1.point[b] != s2.point[(b+begin[s])%6]) { flag = 0; break; } } if(flag == 1) return 1; flag = 1; int c; for(c=0; c<6; c++) { if(s1.point[c] != s2.point[(6+begin[s]-c)%6]) { flag = 0; break; } } if(flag == 1) return 1; } return 0; } int main() { int n; while(scanf("%d",&n)!=EOF) { vector<Snow> v[MAX+4]; int i,j,sum=0; for(i=0;i<n;i++) { for(j=0;j<6;j++) { scanf("%d",&snow[i].point[j]); } int S; S=Hash(snow[i]); v[S].push_back(snow[i]); } for(int q=0;q<MAX+4;q++) { for(int w=0;w<v[q].size();w++) { for(int e=w+1;e<v[q].size();e++) { if(compare(v[q][w],v[q][e])==1) { sum=1; printf("Twin snowflakes found."); printf("\n"); //return 0; } } } } if(sum==0) { printf("No two snowflakes are alike."); printf("\n"); } } return 0; }