poj 3349 Snowflake Snow Snowflakes
2015-07-21 09:51
861 查看
题目链接:http://poj.org/problem?id=3349
题 意:给你几组输入(六个数代表雪花的六个角),判断是否有相同的雪花。
思 路:使用哈希查找的方法将数据存入数组,若储存时有冲突发生,则已找到相同的雪花,后面的数据就都不需要处理。
注 :存储时,我使用的是数组的存储方式,并且将数组循环处理了。
原因:比如输入的是1 2 3 4 5 6,则2 3 4 5 6 1,3 4 5 6 1 2,……,6 5 4 3 2 1,5 4 3 2 1 6等都是相同形状的。
代码如下:
题 意:给你几组输入(六个数代表雪花的六个角),判断是否有相同的雪花。
思 路:使用哈希查找的方法将数据存入数组,若储存时有冲突发生,则已找到相同的雪花,后面的数据就都不需要处理。
注 :存储时,我使用的是数组的存储方式,并且将数组循环处理了。
原因:比如输入的是1 2 3 4 5 6,则2 3 4 5 6 1,3 4 5 6 1 2,……,6 5 4 3 2 1,5 4 3 2 1 6等都是相同形状的。
代码如下:
#include <iostream> using namespace std; #include <string.h> #include <stdio.h> #include <queue> #include <algorithm> #define M 1200010 #define m 1200007 typedef long long LL; struct node { int num[6]; LL next; }; node nu[M]; LL hashx[m], cur; int gethash(int *num1) { int has = 0; for( int i = 0; i < 6; i ++ ) has += num1[i]; return has%m; } int bijiao( int *num1, int *num2 ) { for( int i = 0; i < 6; i ++ ) if( num1[i] != num2[i] ) return 0; return 1; } void inserthash( int *num1, int h ) { for( int i = 0; i < 6; i ++ ) nu[cur].num[i] = num1[i]; nu[cur].next = hashx[h]; hashx[h] = cur; cur++; } int searchhash( int *num1 ) { int h = gethash(num1); int next = hashx[h]; while( next != -1 ) { if( bijiao( num1, nu[next].num ) ) return 1; next = nu[next].next; } inserthash(num1,h); return 0; } int main() { int flag = 0, num1[2][12]; LL n ; cur = 0; scanf ( "%lld", &n ); for( int i = 0; i < m; i ++ ) hashx[i]=-1; while( n-- ) { for( int i = 0; i <6; i ++ ) { scanf ( "%d", &num1[0][i] ); num1[0][i+6]=num1[0][i]; } if( flag == 1 ) continue; for( int i = 0; i < 6; i ++ ) num1[1][i+6] = num1[1][i] = num1[0][5-i]; for( int i = 0; i < 6; i ++ ) { if( searchhash(num1[0]+i) || searchhash( num1[1]+i) ) { flag = 1; break; } } } if( flag ) printf("Twin snowflakes found.\n"); else printf("No two snowflakes are alike.\n"); return 0; }
相关文章推荐
- Javascript SHA-1:Secure Hash Algorithm
- [转]可视化的数据结构和算法
- 统计文件中不小于某一长度的单词的个数(泛型算法实现)
- 使用他人的MD5编码类,修改形成密码串
- Extracting Structured Data from Web Pages
- (译)Cocos2d_for_iPhone_1_Game_Development_Cookbook:1.13使用CCTexture2DMutable调换调色盘
- Java中3DES加密
- Refactoring Notes-Refactoring Methods(3)
- 图书馆管理程序~~不过貌似功能!!有空再修修
- trainging contest#2(2011成都现场赛)I BY Hyoga
- C/C++头文件包含内容概览
- 堆栈的应用(1) 平衡符号 C++实现
- 程序员编程艺术第一章、左旋转字符串
- 程序员编程艺术:第三章续、Top K算法问题的实现
- 程序员编程艺术:第四章、现场编写类似strstr/strcpy/strpbrk的函数
- 十四、第三章再续:快速选择SELECT算法的深入分析与实现
- 程序员编程艺术:第七章、求连续子数组的最大和
- 程序员编程艺术:第八章、从头至尾漫谈虚函数
- 程序员编程艺术:第九章、闲话链表追赶问题
- 程序员编程艺术:第十章、如何给10^7个数据量的磁盘文件排序