数据太大,为了增加查找的效率,直接用hash来进行处理,其实就是一个2维的表。
#include<cstdio> #include<cstring> #include<iostream> #include<cstdlib> #define MOD 10001 using namespace std; int n; typedef struct node{ int arm[6]; struct node *next; }Node; Node arr[MOD]; void ini() { for(int i=0; i<MOD; ++i) { arr[i].next = NULL; } } bool judge(Node a,Node b) { for(int i=0; i<6; ++i) if(a.arm[0]==b.arm[i]) { int j; for(j = 1; j < 6; ++j) if(a.arm[j] != b.arm[(i+j)%6]) break; if(j==6) return true; for(j = 1; j < 6; ++j) if(a.arm[6-j] != b.arm[(i+j)%6]) break; if(j==6) return true; } return false; } int main(void) { cin>>n; ini(); bool flag = false; for(int i = 1; i <= n; ++i) { Node now,next; int sum = 0; for(int j = 0; j < 6; ++ j) { scanf("%d",&now.arm[j]); sum += now.arm[j]; } int hash = sum % MOD; Node * point = arr[hash].next; if(!flag) { while(point) { if(judge(*point,now)) { flag = true; break; } point = point->next; } point = &arr[hash]; Node *tmp = (Node *)malloc(sizeof(Node)); *tmp = now; tmp->next = point->next; point->next = tmp; } } if(flag) printf("Twin snowflakes found.\n"); else printf("No two snowflakes are alike.\n"); return 0; }