poj-3349 Snowflake Snow Snowflakes

来源:互联网 发布:侠客风云传内存优化 编辑:程序博客网 时间:2024/06/11 08:44
A - Snowflake Snow Snowflakes
Time Limit:4000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status

Description

You may have heard that no two snowflakes are alike. Your task is to write a program to determine whether this is really true. 

Your program will read information about a collection of snowflakes, and search for a pair that may be identical. Each snowflake

 has six arms. For each snowflake, your program will be provided with a measurement of the length of each of the six arms. 

Any pair of snowflakes which have the same lengths of corresponding arms should be flagged by your program as possibly identical.

Input

The first line of input will contain a single integer n, 0 < n ≤ 100000, the number of snowflakes to follow. This will be followed by n lines, 

each describing a snowflake. Each snowflake will be described by a line containing six integers (each integer is at least 0 and less than 10000000),

 the lengths of the arms of the snow ake. The lengths of the arms will be given in order around the snowflake (either clockwise or counterclockwise), 

but they may begin with any of the six arms. For example, the same snowflake could be described as 1 2 3 4 5 6 or 4 3 2 1 6 5.

Output

If all of the snowflakes are distinct, your program should print the message:
No two snowflakes are alike.
If there is a pair of possibly identical snow akes, your program should print the message:
Twin snowflakes found.

Sample Input

21 2 3 4 5 64 3 2 1 6 5

Sample Output

Twin snowflakes found.

解题报告:

1,题目描述:输入多组学花的六个片,寻找是否存在两个一样的雪花,

(雪花一样是逆序相同或顺序相同);

2,解题思路:利用求和取模建立hash表,采用拉链法解决hash冲突。

具体思路,要想判断两片雪花是否相等首先要判断他们的花瓣长度和是否相等,

只有满足和相等,在进一步判断他们是否每片花瓣顺序或逆序相等。

代码:

#include <iostream>#include <cstdio>using namespace std;const int prime=99997;struct hashnode{   int len[6];   hashnode *next;};hashnode hashtable[100001]={0};bool clockwise(hashnode *p,hashnode *q)//判断顺序是否相等{    int i,j;    for(i=0;i<6;i++)    {        for(j=0;j<6;j++)        {           if(p->len[j]!=q->len[(i+j)%6])            break;        }        if(j>=6)            break;    }        if(i<6)            return true;        else            return false;}bool counterclockwise(hashnode *p,hashnode *q)//判断逆序是否相等{    int i,j;    for(i=0;i<6;i++)    {                for(j=0;j<6;j++)        {           if(p->len[j]!=q->len[(i+6-j)%6])            break;        }        if(j>=6)            break;    }        if(i<6)            return true;        else            return false;}bool find(hashnode *p,int key)//寻找是否有两片雪花顺序或逆序相等{    if(hashtable[key].next==NULL)//判断当前位置是否存在hash冲突    {        hashtable[key].next=p;//若不存在则把该数存入        return false;//失败返回    }    else//如果该位置存在hash冲突,则拉链存入,并对该链里的雪花进行逐一的比较    {        hashnode *q=&hashtable[key];//把当前输入的节点放在该链的前端        p->next=q->next;        q->next=p;        q=p->next;//从该节点的后一个节点开始搜索直至链尾,,        while(q)        {            if(clockwise(p,q) || counterclockwise(p,q))//判断两片花瓣是否顺序或逆序相等                return true;//找到则返回真            q=q->next;        }        return false;//否则返回假    }}int main(){    int n,i,key;    bool f=false;    cin>>n;    while(n--)    {        key=0;        hashnode *p=new hashnode;        p->next=NULL;        for(i=0;i<6;i++)//求关键字key,求和取余法        {            scanf("%d",&p->len[i]);            key=(key+p->len[i])%prime;        }        if(find(p,key))//判断是否找到了两个相同的雪花,若找到f标记为真,        {            f=true;            break;//在这里可以直接结束,节省时间。        }    }    if(f)        printf("Twin snowflakes found.\n");        else            printf("No two snowflakes are alike.\n");            return 0;}



0 0
原创粉丝点击