码迷,mamicode.com
首页 > 其他好文 > 详细

POJ3349 Snowflake Snow Snowflakes [哈希]

时间:2019-10-06 11:22:25      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:ios   没有   相同   stream   ike   string   memcpy   mem   ons   

POJ 3349

题意:给出n瓣雪花,每片雪花有六瓣,六瓣花瓣的长度按顺时针或逆时针给出,判断其中有没有相同的雪花(六瓣花瓣的长度相同)

相同的雪花六个边长度的和、积应该相同,hash。

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 100010;
const int P = 99991;

int snow[MAXN][6],n,tot,hd[MAXN],nxt[MAXN];

int H(int *a){
    int sum = 0,mul = 1;
    for(int i = 0; i < 6; i++){
        sum = (sum + a[i]) % P;
        mul = ((long long)a[i] * mul) % P;
    }
    return (sum + mul) % P;
}
int equal(int *a,int *b){
    for(int i = 0; i < 6; i++)
        for(int j = 0; j < 6; j++){
            int eq = 1;
            for(int k = 0 ; k < 6; k++)
                if(a[(i + k) % 6] != b[(j + k) % 6]) eq = 0;
            if(eq) return 1;
            
            eq = 1;
            for(int k = 0 ; k < 6; k++)
                if(a[(i + k) % 6] != b[(j - k + 6) % 6]) eq = 0;
            if(eq) return 1; 
        }
    return 0;
}

int insert(int *a){
    int val = H(a);
    for(int i = hd[val]; i; i = nxt[i])
        if(equal(a,snow[i])) return 1;
    tot++;
    memcpy(snow[tot],a,6 * sizeof(int));
    nxt[tot] =  hd[val];
    hd[val] = tot;
    return 0;
}

int main(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        int a[10] = {0};
        for(int j = 0; j < 6; j++) scanf("%d",&a[j]);
        if(insert(a)){
            cout << "Twin snowflakes found." << endl;
            return 0;
        }
    }
    cout << "No two snowflakes are alike." << endl;
    return 0;
}

POJ3349 Snowflake Snow Snowflakes [哈希]

标签:ios   没有   相同   stream   ike   string   memcpy   mem   ons   

原文地址:https://www.cnblogs.com/FoxC/p/11626596.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!