poj 3349 Snowflake&nbs…

10 篇文章 0 订阅
10 篇文章 0 订阅
判断两个雪花是否一样
n=100000;
对每个雪花做一次hash,hash值相同的建立一个链表。
用的是数组模拟链表的方法。
hash函数也用的很简单的办法,全部相加然后求模

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn=5111111;
int next[maxn];

int lon;
struct
{
    int next;
    int a[7];
}e[1111111];

int cmp(int tmp[],int txt[])
{
    int ture;
    for(int k=0;k<6;k++)
    {
        ture=1;
        for(int i=1;i<=6;i++)
        if(tmp[i]!=txt[(i+k-1)%6+1])
        {
            ture=0;
            break;
        }
        if(ture)
        return(1);
    }

    for(int k=0;k<6;k++)
    {
        ture=1;
        for(int i=1,j=6;i<=6;i++,j--)
        if(tmp[j]!=txt[(i+k-1)%6+1])
        {
            ture=0;
            break;
        }
        if(ture)
        return(1);
    }
    return(0);
}


int check(int tmp[])
{
    int txt=0;
    for(int i=1;i<=6;i++)
    txt+=tmp[i];
    txt%=maxn;

    if(next[txt]==-1)
    {
        next[txt]=++lon;
        for(int i=1;i<=6;i++)
        e[lon].a[i]=tmp[i];
    }
    else
    {
        int u=next[txt];
        while(e[u].next!=0)
        {
            if(cmp(tmp,e[u].a)==1)
            return(1);
            u=e[u].next;
        }
            if(cmp(tmp,e[u].a)==1)
            return(1);
            e[u].next=++lon;
            for(int i=1;i<=6;i++)
            e[lon].a[i]=tmp[i];
    }
    return(0);
}

int main()
{
    int n;
    scanf("%d",&n);
    int tmp[10];
    memset(next,-1,sizeof(next));
    memset(e,0,sizeof(e));
    lon=0;
    int i,ture=0;
    for(i=1;i<=n;i++)
    {
        for(int k=1;k<=6;k++)
        scanf("%d",&tmp[k]);
        if(check(tmp)==1)
        {
            ture=1;
            break;
        }
    }
    for(i++;i<=n;i++)
    for(int k=1;k<=6;k++)
    scanf("%d",&tmp[k]);

    if(ture)
    printf("Twin snowflakes found.\n");
    else
    printf("No two snowflakes are alike.\n");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值