Snowflake Snow Snowflakes (POJ - 3349 ,Hash表查找)

一.题目链接:

POJ-3349

二.题目大意:

有 n 片雪花,每片雪花有六个角,若六个角均相同,则称两片雪花相等.

雪花六个角的记录可能顺时针,可能逆时针,开始点不确定.

让你判断是否存在两片相同的雪花.

三.分析:

居然还有考 Hash 表的题 一直觉得数据结构课本很鸡肋

题目不难,这里只是存个板子.

哈希函数选用除留余数法,利用链地址法处理冲突.

四.代码实现:

#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-8
#define lc k * 2
#define rc k * 2 + 1
#define pi acos(-1.0)
#define ll long long int
using namespace std;

const int M = (int)3e5;
const int mod = 99991;
const int inf = 0x3f3f3f3f;

int a[6], b[6];

struct node
{
    int s[6];
}tmp;
vector <node> v[mod];

int get_h()
{
    int sum = 0;
    for(int i = 0; i < 6; ++i)
        sum = (sum + a[i]) % mod;
    return sum % mod;
}

bool Equal()
{
    for(int i = 0; i < 6; ++i)
    {
        bool flag = 1;
        for(int j = 0; j < 6; ++j)
        {
            if(a[j] != b[(i + j) % 6])
            {
                flag = 0;
                break;
            }
        }
        if(flag)
            return 1;
    }
    reverse(b, b + 6);
    for(int i = 0; i < 6; ++i)
    {
        bool flag = 1;
        for(int j = 0; j < 6; ++j)
        {
            if(a[j] != b[(i + j) % 6])
            {
                flag = 0;
                break;
            }
        }
        if(flag)
            return 1;
    }
    return 0;
}

bool Insert()
{
    int h = get_h();
    int len = v[h].size();
    for(int i = 0; i < len; ++i)
    {
        memcpy(b, v[h][i].s, sizeof(v[h][i].s));
        if(Equal())
            return 1;
    }
    memcpy(tmp.s, a, sizeof(a));
    v[h].push_back(tmp);
    return 0;
}

/**
2
1 2 3 4 5 6
4 3 2 1 6 5
**/

int main()
{
    int n;
    scanf("%d", &n);
    bool flag = 0;
    for(int i = 0; i < n; ++i)
    {
        for(int j = 0; j < 6; ++j)
            scanf("%d", &a[j]);
        if(flag)
            continue;
        if(Insert())
            flag = 1;
    }
    if(flag)
        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、付费专栏及课程。

余额充值