poj3349

来源:互联网 发布:软件操作说明书模版 编辑:程序博客网 时间:2024/05/02 04:41
#include<stdio.h>
#include<malloc.h>
#include<string.h>


typedef struct Node
{
int anxis[6];
struct Node *next;
}Node;


void addNode(Node *data[10000],Node *to_add,int *flag);
void cmp(Node *head,Node *to_add,int *flag);


void cmp(Node *head,Node *to_add,int *flag)
{
//比较head-》的节点和to—add指向的节点
int i;
int j;
int t;
for(i=0; i<6 && !(*flag); i++)
{
j = 0;
if(head->anxis[i] == to_add->anxis[j])
{
t = i;
//顺时针和逆时针处理
while(j<6)
{
if(head->anxis[t] == to_add->anxis[j])
{
t=(t+1)%6,j++;
}
else
{
break;
}
}
if(j==6)
(*flag) = 1;
t = i;
j=0;
while(j<6)
{
if(head->anxis[t] == to_add->anxis[j])
{
t = (t-1<0) ? t+5 : t-1;
j++;
}
else
{
break;
}
}
if(j==6)
(*flag) = 1;
}
}
}


void addNode(Node *data[10000],Node *to_add,int *flag)
{
int i;
int hashNum=0;
Node *head;
Node *pre;


//计算哈希值确定下标
// for(i=0; i<6; i++)
// hashNum = hashNum*7+to_add->anxis[i];
// hashNum = hashNum<0?((-hashNum)%1000):(hashNum%1000);

for(i=0; i<6; i++)
hashNum += to_add->anxis[i];
hashNum = hashNum%10000;


if(data[hashNum] == NULL)
{
data[hashNum] = to_add;
}
else
{
pre = data[hashNum];
head = data[hashNum];
while(head != NULL)
{
//没找到则继续判断
if(!(*flag))
cmp(head,to_add,flag);
pre = head;
head = head->next;
}
pre->next =to_add;
}
}
int main(void)
{
int i,j;
int count;
int flag=0;
Node *data[10000];
Node *t;
memset(data,0,sizeof(Node *)*10000);
scanf("%d",&count);
for(i=0; i<count && (!flag); i++)
{
t = (Node *)malloc(sizeof(Node) * 1);
t->next = NULL;
for(j=0; j<6; j++)
{
scanf("%d",&(t->anxis[j]));
}
addNode(data,t,&flag);
}
if(flag)
printf("Twin snowflakes found.");
else
printf("No two snowflakes are alike.");
}
0 0
原创粉丝点击