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

    题意:一个雪花有六个角  给出N个雪花 判断有没有相同的(可以随意旋转)

    参考:https://blog.csdn.net/alongela/article/details/8245005  

    注意:参考的博客的写法有问题  不知道POJ为什么没有卡掉

    例如 数据         1

                            1 1 1 1 1 1 

    如果用这个博客的写法 等于自己和自己比了 输出的是yes

    只要稍微改一下  改成比完之后插入key就能解决了

    疑问:  manx=1000000+10会T 改成1200000+10才能过不知道为什么

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    const int maxn=1200010;
    const int MOD=1200007;
    int HashTable[maxn];
    int cur=0;
    struct Node{
        int num[7];
        int next;
    }node[MOD+10];
    
    unsigned  getHash(int*num){
        unsigned  sum=0;
        for(int i=0;i<6;i++)
            sum=(sum+num[i])%MOD;
        return sum%MODhttps:
    }
    void insertHash(int*num,unsigned key){
        for(int i=0;i<6;i++)node[cur].num[i]=num[i];
        node[cur].next=HashTable[key];
        HashTable[key]=cur;
        cur++;
    }
    bool cmp(int*num1,int*num2){
        for(int i=0;i<6;i++){
            if(num1[i]!=num2[i])return 0;
        }
        return 1;
    }
    bool searchHash(int*num){
          unsigned key=getHash(num);
          int next=HashTable[key];
          while(next!=-1){
              if(cmp(num,node[next].num))return 1;
              next=node[next].next;
          }
          //insertHash(num,key);//不能边查边插,否则会自己匹配自己
          return 0;
    }
    int main(){
        int n;
        int num[2][300];
        bool flag=0;
        cur=0;
        memset(HashTable,-1,sizeof(HashTable));
        scanf("%d",&n);
        while(n--){
            for(int i=0;i<6;i++){
                scanf("%d",&num[0][i]);
                num[0][i+6]=num[0][i];
            }
            if(flag)continue;
            for(int i=0;i<6;i++){
                num[1][i+6]=num[1][i]=num[0][5-i];
            }
            for(int i=0;i<6;i++){
                if(searchHash(num[0]+i)||searchHash(num[1]+i)){
                    flag=1;
                //    cout<<111<<endl;
                    break;
                }
            }
            for(int i=0;i<6;i++){//这里改成了之后插入  
                insertHash(num[0]+i,getHash(num[0]+i));
                    insertHash(num[1]+i,getHash(num[1]+i));
            }
        }
        if(flag)printf("Twin snowflakes found.
    ");
        else printf("No two snowflakes are alike.
    ");
    
        return 0;
    }
  • 相关阅读:
    HDU 3572 Task Schedule(拆点+最大流dinic)
    POJ 1236 Network of Schools(Tarjan缩点)
    HDU 3605 Escape(状压+最大流)
    HDU 1166 敌兵布阵(分块)
    Leetcode 223 Rectangle Area
    Leetcode 219 Contains Duplicate II STL
    Leetcode 36 Valid Sudoku
    Leetcode 88 Merge Sorted Array STL
    Leetcode 160 Intersection of Two Linked Lists 单向链表
    Leetcode 111 Minimum Depth of Binary Tree 二叉树
  • 原文地址:https://www.cnblogs.com/ttttttttrx/p/10040836.html
Copyright © 2011-2022 走看看