题意:给出n瓣雪花,每片雪花有六瓣,六瓣花瓣的长度按顺时针或逆时针给出,判断其中有没有相同的雪花(六瓣花瓣的长度相同)
相同的雪花六个边长度的和、积应该相同,hash。
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 100010;
const int P = 99991;
int snow[MAXN][6],n,tot,hd[MAXN],nxt[MAXN];
int H(int *a){
int sum = 0,mul = 1;
for(int i = 0; i < 6; i++){
sum = (sum + a[i]) % P;
mul = ((long long)a[i] * mul) % P;
}
return (sum + mul) % P;
}
int equal(int *a,int *b){
for(int i = 0; i < 6; i++)
for(int j = 0; j < 6; j++){
int eq = 1;
for(int k = 0 ; k < 6; k++)
if(a[(i + k) % 6] != b[(j + k) % 6]) eq = 0;
if(eq) return 1;
eq = 1;
for(int k = 0 ; k < 6; k++)
if(a[(i + k) % 6] != b[(j - k + 6) % 6]) eq = 0;
if(eq) return 1;
}
return 0;
}
int insert(int *a){
int val = H(a);
for(int i = hd[val]; i; i = nxt[i])
if(equal(a,snow[i])) return 1;
tot++;
memcpy(snow[tot],a,6 * sizeof(int));
nxt[tot] = hd[val];
hd[val] = tot;
return 0;
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
int a[10] = {0};
for(int j = 0; j < 6; j++) scanf("%d",&a[j]);
if(insert(a)){
cout << "Twin snowflakes found." << endl;
return 0;
}
}
cout << "No two snowflakes are alike." << endl;
return 0;
}