POJ3349 Snowflake Snow Snowflakes

本文介绍了一个判断雪花图案是否相似的问题及解决方案。通过哈希映射和冲突检查来确定多个六边形雪花中是否存在完全相同的两个。算法使用了旋转和平移不变性的特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://poj.org/problem?id=3349


题目大意:

给你n个6条边的雪花的信息,问这些雪花当中是否有相同的一对。

相同的定义是:长度相同,且符合相同的顺序(顺时针或者逆时针),比如1 2 3 4 5 6是a雪花的6边信息,那么相同的雪花共有12种情况,就是按照顺时针和逆时针和a雪花边相等且顺序相同。如顺时针可以是:2 3 4 5 6 1或者3 4 5 6 1 2。逆时针可以是6 5 4 3 2 1 或者4 3 2 1 6 5.


题解:将每个雪花哈希映射到表中,如果出现冲突则判断是否出现了雪花相同的情况,没有则用拉链法解决冲突。


#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const double eps=1e-8;
const double INF=1e50;
//const double pi=acos(-1);

#define N 10007
struct ss
{
    int a[7];
}b;
vector<struct ss>p[N];
int n;

int c[20],c1[20];

bool find()
{
    int i,j;
    for (i=1;i<=7;i++)
    if (c[i]==c1[1])
    {
        for (j=2;j<=6;j++)
        if (c[i+j-1]!=c1[j]) break;
        if (j==7) return true;
    }

    return false;
}

bool check(int x)
{
    int i,j,k;
    for (i=0;i<p[x].size();i++)
    {
        for (j=1;j<=6;j++)
        {c[j]=p[x][i].a[j];c1[j]=b.a[j];}
        for (j=1;j<=6;j++) c[j+6]=p[x][i].a[j];

        if (find()==true) return false;
        for (j=1;j<=3;j++)
        {
            k=c1[j];
            c1[j]=c1[6-j+1];
            c1[6-j+1]=k;
        }
        if (find()==true) return false;
    }

    return true;
}

int main()
{
    freopen("a","r",stdin);

    int i,hash,j,k;
    scanf("%d\n",&n);
    bool flag=true;
    for (k=1;k<=n;k++)
    {
        hash=0;
        for (i=1;i<=6;i++)
        {
            scanf("%d",&b.a[i]);
            hash+=b.a[i];
            hash%=N;
        }

        if (p[hash].size()==0) p[hash].push_back(b);
        else if (check(hash)==true) p[hash].push_back(b);
        else
        {
            flag=false;
            break;
        }
    }

    for (j=k;j<=n;j++)
    for (i=1;i<=6;i++) scanf("%d",&b.a[i]);

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

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值