/* Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K 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. FoRever:: memory_used 8800K time_used 2400MS 高内存高用时飘过...而且换了个相乘的hash函数就RE了... 看来对时间还是扣得很严的,我算法优化的也不是很好..凑合看吧 这一题是简单HASH,题意大概就是给你N个雪花,雪花有6个角(也就是6个值),判断雪花是否相等 ,顺序逆序都要考虑; 思路就是把雪花的六个角相加作为hash值,再将雪花的六个角分别存入链表中,分别查找 这里链表我用了一个模板,感觉非常不错...推荐一下 在雪花判断的时候我用了12个暴力判断... 比较坑爹,代码太长,肯定有优化算法的,不过我没有多想,简单题就直接水过吧 ~~~~1次AC...感觉颇爽~~~~ */ #include<stdio.h> #include<math.h> const int H = 99991; const int MAX = 100001; int hash(int a,int b,int c,int d,int e,int f) //hash函数,应该加起来是比较好的...因为雪花有顺序,用加法 || 乘法|| 平方 || 开方 才不会改变他们的hash值 { return (a+b+c+d+e+f)%H; } struct snow //这个数组链表是之前用过的,int存储雪花的六个角,value表示访问情况,*next不解释 { int a,b,c,d,e,f; bool value; snow *next; }s[MAX]; void insert(snow *p,int a,int b,int c,int d,int e,int f) //插入函数,将雪花的六个角插入链表 { if(p->value == false) { p->a = a ; p->b = b; p->c = c ; p->d = d; p->e = e ; p->f = f; p->value = true; p->next = new snow; p->next->value = false; //new出链表的下一个,并设置访问值为false }else { p = p->next; insert(p,a,b,c,d,e,f); //递归,直到找到插入点 } } bool find(int a,int b,int c,int d,int e,int f) //查找函数,暴力查找,顺序逆序一共12个 { snow *p = &s[hash(a,b,c,d,e,f)]; while(p->value == true) //是否hash值已经存在,是的话就依次判断 { if(p->a == a && p->b == b && p->c == c && p->d == d && p->e == e && p->f == f) {return true;} else if(p->a == b && p->b == c && p->c == d && p->d == e && p->e == f && p->f == a) {return true;} else if(p->a == c && p->b == d && p->c == e && p->d == f && p->e == a && p->f == b) {return true;} else if(p->a == d && p->b == e && p->c == f && p->d == a && p->e == b && p->f == c) {return true;} else if(p->a == e && p->b == f && p->c == a && p->d == b && p->e == c && p->f == d) {return true;} else if(p->a == f && p->b == a && p->c == b && p->d == c && p->e == d && p->f == e) {return true;} //这里是顺序 else if(p->a == a && p->b == f && p->c == e && p->d == d && p->e == c && p->f == b) {return true;} else if(p->a == b && p->b == a && p->c == f && p->d == e && p->e == d && p->f == c) {return true;} else if(p->a == c && p->b == b && p->c == a && p->d == f && p->e == e && p->f == d) {return true;} else if(p->a == d && p->b == c && p->c == b && p->d == a && p->e == f && p->f == e) {return true;} else if(p->a == e && p->b == d && p->c == c && p->d == b && p->e == a && p->f == f) {return true;} else if(p->a == f && p->b == e && p->c == d && p->d == c && p->e == b && p->f == a) {return true;} //这里是逆序 else p = p -> next; } return false; } int main() { int n ; scanf("%d",&n); int i; for(i = 0 ; i < MAX ; i++ ) s[i].value = false; bool judge = false; //雪花相同的判断变量 for(i = 0 ; i < n ;i++) { int a,b,c,d,e,f; scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f); //每输入一次都查找一次 if(judge == false)//加这个判断的话,如果已经找到相同雪花就不用进入判断了 { if(find(a,b,c,d,e,f) == true) judge = true; insert(&s[hash(a,b,c,d,e,f)],a,b,c,d,e,f); } } if(judge == false) printf("No two snowflakes are alike.\n"); else printf("Twin snowflakes found.\n"); return 0; }
POJ 3349 Snowflake Snow Snowflakes Hash
最新推荐文章于 2022-02-25 19:38:17 发布