POJ 3349 - Snowflake Snow Snowflakes(Hash)

来源:互联网 发布:传奇引擎源码 编辑:程序博客网 时间:2024/06/09 08:09

题目:

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

题意:

给出n片六角雪花每一个角的长度, 判断是否存在两篇相似的雪花.

思路:

Hash, Key = 雪花六角长度想加%100003.
若两片雪花key相同, 则判断两片雪花是否相似, 顺时针和逆时针都要判断.

CODE:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn = 100003;int head[maxn], next[maxn];int a[maxn][10];bool Hash(int e, int it){    bool ok = 0;    for(int i = 0; i < 6; ++i) {        ok = 1;        for(int j = i, t = 0; t < 6; t++, j = (j+1)%6) {            if(a[e][t] != a[it][j]) {                ok = 0;                break;            }        }        if(ok) return true;    }    for(int i = 0; i < 6; ++i) {        ok = 1;        for(int j = i, t = 0; t < 6; ++t) {            if(a[e][t] != a[it][j]) {                ok = 0;                break;            }            if((--j) < 0) j = 5;        }        if(ok) return true;    }    return false;}int main(){//freopen("in", "r", stdin);    int n;    scanf("%d", &n);    memset(head, -1, sizeof(head));    bool ok = 0;    for(int i = 0; i < n; ++i) {        int s = 0;        for(int j = 0; j < 6; ++j) {            scanf("%d", &a[i][j]);            if(ok) continue;            s += a[i][j];        }        if(ok) continue;        s = s % maxn;        for(int e = head[s]; e != -1; e = next[e]) {            if(Hash(e, i)) ok = 1;        }        if(!ok) {            next[i] = head[s];            head[s] = i;        }    }    if(ok) printf("Twin snowflakes found.\n");    else printf("No two snowflakes are alike.\n");    return 0;}


0 0
原创粉丝点击