Snowflake Snow Snowflakes POJ

来源:互联网 发布:知乎中国人到底有多穷 编辑:程序博客网 时间:2024/06/06 01:27


题意是给出一组雪花的6个角的值,判断通过比较是否能发现两片相同的雪花。


主要利用了hash表的映射,计算每个雪花的key值,如果不存在以此key值为键的hash表,则新建一个,若存在则查找hash表,

如果有则标记Find为ture

否则通过hash重映射方法,继续向以此key值为键的hash表添加数据。


判断的时候需要分顺时针和逆时针两种比较方法。

#include<cstdio>#include<iostream>#include<cstring>using namespace std;int Find;const __int64 prime =999983;class{public:    __int64 len[6];} leaf[100001];typedef class HashTable{public:    __int64 len[6];    HashTable *next;    HashTable()    {        next=0;    }} HashTable;HashTable* hash[prime+1];bool clock(HashTable *p,int k){    //顺时针比较    for(int i=0; i<6; ++i)    {        bool flag=true;        for(int j=0; j<6; ++j)        {            if(leaf[k].len[j] != p->len[(i+j)%6])            {                flag=false;                break;            }        }        if(flag)            return true;    }    //逆时针比较    for(int i=0; i<6; ++i)    {        bool flag=true;        for(int j=0; j<6; ++j)        {            if(leaf[k].len[j] != p->len[(5-i-j+6)%6])            {                flag=false;                break;            }        }        if(flag)            return true;    }    return false;}__int64 compute_key(int k){    __int64 key=0;    for(int i=0; i<6; ++i)    {        key+=(leaf[k].len[i])%prime;        key%=prime;    }    return ++key;}void insert(int k){    __int64 key=compute_key(k);    if(!hash[key])    {        HashTable *temp=new HashTable;        for(int i=0; i<6; ++i)            temp->len[i]=leaf[k].len[i];        hash[key]=temp;    }    else    {        HashTable *temp=hash[key];        if(clock(temp,k))        {            Find=1;            return;        }        while(temp->next)        {            temp=temp->next;            if(clock(temp,k))            {                Find=1;                return;            }        }        temp->next=new HashTable;        for(int i=0; i<6; i++)            temp->next->len[i]=leaf[k].len[i];    }}int main(){    int n;    cin>>n;    Find=0;    memset(hash,0,sizeof(hash));    for(int i=1; i<=n; ++i)    {        for(int j=0; j<6; ++j)            scanf("%I64d",&leaf[i].len[j]);        if(!Find)        {            insert(i);        }    }    if(Find)        cout<<"Twin snowflakes found."<<endl;    else        cout<<"No two snowflakes are alike."<<endl;}


原创粉丝点击