zoukankan      html  css  js  c++  java
  • Snowflake Snow Snowflakes POJ

    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.


    题意:给n朵雪花,每个雪花都有6条边,找出是否有两个相同的雪花。
    思路:n²遍历肯定不行,然后就想到hash,对于存放hash值的邻接表,我用的是vector,事实证明很慢啊,手写前向星快很多。
    hash:我用的是直接相加然后取模一个素数的方法,当然还有很多其他方法,各种异或或者骚运算应该都差不多。

    (对于equl函数,我错了很久,之前写的是用while找到一个pos位置和b【0】相同,就退出,然后我发现,找到一个不够,也许有下一个不能直接退出)
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    using namespace std;
    
    const int SIZE = 100010;
    int n,tot,p=99991;
    int snow[SIZE][6];
    vector<int>v[SIZE];
    
    int h(int a)
    {
        int sum=0;
        for(int i=0;i<6;i++)
        {
            sum = (sum + snow[a][i]) % p;
        }
        return sum % p;
    };
    
    bool equl(int a,int b)
    {
       for(int j=0;j<6;j++)
       {
            if(snow[a][j] != snow[b][0])continue;
            int flag =  1;
            for(int i=0; i<6; i++)
            {
                if(snow[a][(j + i)%6] != snow[b][i] )
                    flag = 0;
            }
            if(flag)
                return 1;
            flag = 1;
            for(int i=0; i<6; i++)
            {
                if(snow[a][(j-i+6)%6] != snow[b][i])
                    flag = 0;
            }
            if(flag) return 1;
       }
        return 0;
    }
    
    
    bool Insert(int a)
    {
        int val = h(a);
        int len = v[val].size();
        for(int i=0;i<len;i++)
        {
    
            if(equl(v[val][i],a))return 1;
        }
        v[val].push_back(a);
        return 0;
    }
    
    int main()
    {
        scanf("%d",&n);
        int flag = 0;
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<6;j++)
            {
                scanf("%d",&snow[i][j]);
            }
            if(!flag && Insert(i))
            {
                puts("Twin snowflakes found.");
                flag = 1;
            }
        }
        if(!flag)
        puts("No two snowflakes are alike.");
    }
    View Code
  • 相关阅读:
    请求转发和请求重定向的区别
    查看电脑连过的WiFi密码
    linux mysql不能远程登录
    map的遍历方法
    ________________springbootのMybatis
    ________________springbootのTest
    ________________springbootの自定义starter
    ________________springbootのAOP
    ________________springbootのjdbc、事物
    ________________初学springboot14
  • 原文地址:https://www.cnblogs.com/iwannabe/p/10640268.html
Copyright © 2011-2022 走看看