POJ 3349.Snowflake Snow Snowflakes

550

题目



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  


2
1 2 3 4 5 6
4 3 2 1 6 5


Sample Output  


Twin snowflakes found.


题解



使用哈希表来存储雪花信息
各位长度之和充当哈希函数
当产生冲突时,检验是否相同,相同的话就按照要求输出,否则插入到哈希表里

对于一个指针,sizeof()返回是指针对应的数据类型的长度。
如果是一个指向数组的指针,并不会返回数组长度


代码



/*
By:OhYee
Github:OhYee
Blog:http://www.oyohyee.com/
Email:oyohyee@oyohyee.com

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
using namespace std;


const int maxl = 10000005;
const int maxn = 100005;
const int HashSize = maxn;


int HashTable[HashSize];
int Next[maxn];
int Link[maxn][6];
int pos;

void Init() {
    pos = 0;
    memset(Next,-1,sizeof(Next));
    memset(HashTable,-1,sizeof(HashTable));
}

//hash
bool HashCmp(int a[6],int b[6]) {
    for(int i = 0;i < 6;i++) {
        if(a[i] == b[0]) {
            for(int k = 1;k == 1 || k == -1;k -= 2) {
                for(int j = 1;j < 6;j++) {
                    int n = (i + k*j + 6) % 6;
                    if(a[n] != b[j])
                        break;
                    else
                        if(j == 5)
                            return true;
                }
            }
        }
    }
    return false;
}

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

bool HashInsert(int a[6]) {
    int h = Hash(a);
    if(HashTable[h] == -1) {
        memcpy(Link[pos],a,6 * sizeof(int));
        HashTable[h] = pos++;
    } else {
        int k = HashTable[h];
        int last = HashTable[h];
        while(k != -1) {
            if(HashCmp(a,Link[k]))
                return false;
            last = k;
            k = Next[k];
        }
        memcpy(Link[pos],a,6 * sizeof(int));
        Next[last] = pos++;
    }
    return true;
}

bool Do() {
    int n;
    scanf("%d",&n);
    Init();
    bool Find = false;
    int a[6];
    for(int o = 0;o < n;o++) {
        for(int i = 0;i < 6;i++)
            scanf("%d",&a[i]);
        if(!HashInsert(a)) {
            Find = true;
            break;
        }
    }

    if(Find)
        printf("Twin snowflakes found.\n");
    else
        printf("No two snowflakes are alike.\n");

    return false;
}

int main() {
    while(Do());
    return 0;
}
发布评论
  • 点击查看/关闭被识别为广告的评论