nyoj130 相同的雪花
相同的雪花
时间限制: 1000 ms | 内存限制:65535 KB
难度: 4
The first line of every test case 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. </dd> <dt> 输出 </dt> <dd> For each test case,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. </dd> <dt> 样例输入 </dt> <dd>
1 2 1 2 3 4 5 6 4 3 2 1 6 5</dd> <dt> 样例输出 </dt> <dd>
Twin snowflakes found.</dd> </dl>
解题思路:用到哈希的知识,通过观察可以发现,如果是两片相同的雪花,那么他的和一定是相同的,那就把和当做它的地址来存储,然后如果和相同就都连接在相应的和的后面,采用的是链地址法,然后再从和相同的里面来寻找相同的雪花,就大大的减少了比较次数。
代码:
#include <iostream> #include <cstdio> using namespace std; typedef struct snow{ int a[6]; snow *next; }; snow *s[100000]; int fin(int b[],int sum){ snow *p; p=s[sum]->next; while(p){ for(int i=0;i<6;i++){ int j; if(b[i]==p->a[0]){ for(j=1;j<6;j++){ if(b[i+j]%6!=p->a[j]){ break; } } if(j==6){ return 1; } for(j=1;j<6;j++){ if(b[((i-j%6+6)%6)]!=p->a[j]){ break; } } if(j==6){ return 1; } } } p=p->next; } p=new snow; for(int i=0;i<6;i++){ p->a[i]=b[i]; } p->next=s[sum]->next; s[sum]->next=p; return 0; } int main() { int n; int t; int b[6]; int sum; int flag; scanf("%d",&n); for(int i=0;i<100000;i++){ s[i]=new snow; s[i]->next=NULL; } while(n--){ scanf("%d",&t); flag=0; for(int i=0;i<t;i++){ sum=0; for(int j=0;j<6;j++){ scanf("%d",&b[j]); sum+=b[j]; } sum%=100000; if(!flag){ if(fin(b,sum)){ flag=1; } } } if(flag){ printf("Twin snowflakes found.\n"); }else{ printf("No two snowflakes are alike.\n"); } } return 0; }