雪花
一切尽在代码中
#include<stdio.h>
#include<string.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
using namespace std;
#define N 15000
#define NL 100
#define L 6
typedef struct{//结构体定义雪花六个角
int a[L];
}item;
item snow[N][NL];
int m[N];//存储hash值得位置
int hash(item SNOW)//求hash值,这个是以和为相同再分析相不相同的;
{
int key = 0;
for (int i = 0; i < 6; i++){
key += SNOW.a[i];
}
key %= 14997;
return key;
}
int cmp(item x,item y)//既要左移,又要右移,这样才是完整的
{
int st, i, j;
for (st = 0; st < 6; st++){
for (i = st, j = 0; j < 6; j++, i = (i + 1)%6){//右移
if (x.a[i] != y.a[j])
break;
}
if (j == 6)//当j==6,说明完全一样
return 1;
}
for (st = 0; st < 6; st++){//左移
for (i = st, j = 0; j < 6; j++, i = (i+5)%6){
if (x.a[i] != y.a[j])
break;
}
if (j == 6)
return 1;
}
return 0;
}
int main()
{
int pos, i, j, n;
item SNOW;//相当于temp
scanf("%d",&n);
for (i = 0; i < n; i++){
for (j = 0; j < 6; j++){
scanf("%d",&SNOW.a[j]);
}
pos = hash(SNOW);//求出hash值
for (j = 0; j < m[pos]; j++){
if (cmp(SNOW, snow[pos][j])){
puts("Twin snowflakes found.");
return 0;
}
}
snow[pos][m[pos]] = SNOW;//把新输入的放入snow中,和相同的hash值 pos,然后再在和相同的基础上进行细分
m[pos]++;
}
puts("No two snowflakes are alike.");
return 0;
}