poj 3349 Snowflake Snow Snowflakes

Snowflake Snow Snowflakes
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 29196 Accepted: 7674

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.

输入的第一行包含一个整数n,0 <n≤100000,雪花的数量。这之后,n行,每一行都描述一个雪花。每个雪花将由含有6个整数(每个整数是至少为0并且小于10000000),代表雪花的六瓣长度。臂的长度可以为顺序围绕雪花(顺时针或逆时针)给出,但它们可开始于任何六个臂。例如,相同的雪花可以被描述为1 2 3 4 5 6或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.

Source


额,这是个艰难的时期,这道题做的也十分痛苦最后直接参考某人的程序,写的跟人差不多了,不献丑了,直接贴别人的地址吧

然后我的代码
#include <stdio.h>
#include <string.h>
#include <malloc.h>

int n;
const int primer = 999983;
__int64 leaf[100001][6];

struct Hashtable{
    __int64 len[6];
    struct Hashtable* next;
};

struct Hashtable *hash_t[999984];

__int64 calculate(int i){
    __int64 key = 0;
    int j;
    for (j = 0;j < 6;++ j){
        key += leaf[i][j] % primer;
        key %= primer;
    }
    key++;
//    printf("key = %I64d\n", key);
    return key;

}
int clockwise(struct Hashtable* temp, int i){
    int j, k, flag = 0;
    for (j = 0; j < 6; ++ j){
        flag = 1;
        for (k = 0; k < 6; ++ k){
            if (leaf[i][k] != temp->len[(j+k) % 6]){
                flag = 0;
                break;
            }
        }
        if (flag){
            return 1;
        }
    }
    //printf("ok2\n");
    return 0;
}
int anticlockwise(struct Hashtable* temp, int i){
    int j, k, flag = 0;
//    for (j = 0;j < 6;++ j){
//        printf("temp[%d] = %d\n", j, temp->len[j]);
//    }
    for (j = 0;j < 6;++ j){
        flag = 1;
        for (k = 0;k < 6;++ k){
//            printf("(5-j-k+6) mod 6 = %d\n", (5-j-k+6) % 6);
//            printf("leaf[%d][%d] = %d\n", i, k, leaf[i][k]);
            if (leaf[i][k] != temp->len[(j-k+6) % 6]){
//                printf("(5-j-k+6) % 6 = %d\n", (5-j-k+6) % 6);
                flag = 0;
                break;
            }
        }
        if (flag){
            return 1;
        }
    }
    return 0;
}
int insert_hash(int i){
    int j;
    __int64 key = calculate(i);

    if (hash_t[key] == NULL){
        struct Hashtable* temp=(struct Hashtable*)malloc(sizeof(struct Hashtable));
        if (temp == NULL) return -1;//没能分配到空间,出错
        for (j = 0;j < 6;j ++){
            temp->len[j] = leaf[i][j];
        }
        hash_t[key] = temp;
        temp -> next = NULL;
    }else{
        struct Hashtable* temp=hash_t[key];
        if (clockwise(temp, i) || anticlockwise(temp, i)){
//            printf("ok1\n");
            return 1;
        }
        while(temp->next != NULL){
            temp = temp->next;
            if (clockwise(temp, i) || anticlockwise(temp, i)){
                return 1;
            }
        }
        if ((temp->next = (struct Hashtable*)malloc(sizeof(struct Hashtable)))== NULL){
            return -1;//没能分配到内存
        }
        for (j = 0; j < 6; ++ j){
            temp->next->len[j] = leaf[i][j];
        }
        temp = temp->next;temp->next = NULL;
    }
    return 0;
}

int main(void){
    int i, j;
    int flag = 0;

    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);

    memset(hash_t, 0, sizeof(hash_t));

    scanf("%d", &n);
    for (i = 0;i < n;++ i){
            for (j = 0;j < 6;j ++){
                scanf("%I64d", &leaf[i][j]);
//                printf("%I64d ", leaf[i][j]);
            }
//            printf("\n");
            if (!flag)
                flag = insert_hash(i);
    }
    if (flag){
        printf("Twin snowflakes found.\n");
    }else{
        printf("No two snowflakes are alike.\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值