poj3349

大致题意:给出n片雪花,每片雪花有六个角,每个角由一个数字表示,如雪花A为(1,2,3,4,5,6),问是否有雪花是相同,相同的意思是比如还有雪花B(2,3,4,5,1,6),那么雪花A和B就是一样的

测试案例:

input:

21 2 3 4 5 64 3 2 1 6 5

output:

Twin snowflakes found.


解题思路:我是用纯粹的hash表写的,每次插入雪花时,判断这片雪花是否重复,这次的hash表也是用数组表示的,不过专门弄了个头结点数组和存放节点的数组,判断雪花是否重复我是把雪花的序号按从小到大存入hash表的,所以直接从最小的开比就行了

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

struct node
{
    int snow[6];
    int pos;
};

int head[99991];
node hashtable[100000+10];
int cnt=0;

int main()
{

    int n,i,j,a[6],k,t;

    while (scanf("%d",&n)==1)
    {
        bool flag=true;
        for (i=0;i<99991;i++)
            head[i]=-1;
        for (i=0; i<n; i++)
        {
            hashtable[i].pos=-1;
        }

        for (i=0; i<n; i++)
        {
            scanf("%d%d%d%d%d%d",&a[0],&a[1],&a[2],&a[3],&a[4],&a[5]);
            if (flag)
            {
                sort(a,a+6);
                k=(a[0]+a[1]+a[2]+a[3]+a[4]+a[5])%99991;
                for (t=head[k]; t!=-1; t=hashtable[t].pos)
                {
                    if (hashtable[t].snow[0]==a[0])//我承认我这么写的唯一目的是看上去比较霸气
                    {
                        if (hashtable[t].snow[1]==a[1])
                        {
                            if (hashtable[t].snow[2]==a[2])
                            {
                                if (hashtable[t].snow[3]==a[3])
                                {
                                    if (hashtable[t].snow[4]==a[4])
                                    {
                                        if (hashtable[t].snow[5]==a[5])
                                        {
                                            flag=false;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                for (j=0; j<6; j++)
                {
                    hashtable[cnt].snow[j]=a[j];
                }
                hashtable[cnt].pos=head[k];
                head[k]=cnt;
                cnt++;
            }
        }

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

    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值