#include<iostream>
#include<fstream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
using namespace std;
const int PRIME=999983;
int clockwise[12];
int countclockwise[12];
class Node
{
public:
int arms[6];
Node *next;
bool operator ==(Node p) //比较两个雪花是否相同
{
for(int i=0;i<6;i++)
{
clockwise[i+6]=clockwise[i]=p.arms[i];
countclockwise[i+6]=countclockwise[i]=p.arms[5-i];
}
for(int i=0;i<7;i++)
{
int j;
for(j=0;j<6;j++)
{
if(arms[j]!=clockwise[i+j])
break;
}
if(j==6)
return true;
}
for(int i=0;i<7;i++)
{
int j;
for(j=0;j<6;j++)
{
if(arms[j]!=countclockwise[i+j])
break;
}
if(j==6)
return true;
}
return false;
}
Node(const int *input)
{
for(int i=0;i<6;i++)
{
arms[i]=input[i];
}
next=NULL;
}
};
Node *HashTable[PRIME+1]={};
bool find(int *input)
{
int key=0;
for(int i=0;i<6;i++)
{
key+=input[i];
}
key=key%PRIME+1;
Node *p=HashTable[key];
Node* temp=new Node(input);
if(p==NULL)
{
HashTable[key]=temp;
return false;
}
while(p->next!=NULL)
{
if(*p==*temp)
return true;
p=p->next;
}
if(*p==*temp)
return true;
p->next=temp;
return false;
}
int main()
{
freopen("input.txt","r",stdin);
int n;
bool flag;
int input[6];
scanf("%d",&n);
memset(HashTable,0,sizeof(Node *)*(PRIME+1));
flag=false;
for(int i=0;i<n;i++)
{
for(int j=0;j<6;j++)
scanf("%d",&input[j]);
if(flag==false)
{
flag=find(input);
}
}
if(flag)
printf("Twin snowflakes found.\n");
else
printf("No two snowflakes are alike.");
}
poj 3349
最新推荐文章于 2018-10-16 20:59:07 发布