zoukankan      html  css  js  c++  java
  • poj 3349

    看了大神的博客:http://user.qzone.qq.com/289065406/blog/1304831877

    题意:  在n (n<100000)个雪花中判断是否存在两片完全相同的雪花,每片雪花有6个角,每个角的长 度限制为1000000两片雪花相等的条件:雪花6个角的长度按顺序相等(这个顺序即可以是顺时针的也可以是逆时针的)

    思路;哈希查找

    #include<iostream>
    #include<cstring>
    #include<stdio.h>
    using namespace std;
    struct Node
    {
        int x;
        Node *next;
    }hash[1000000];
    int prime=999983;
    int b;
    struct flower
    {
        int key;
        int a[7];
    }f[100001];
    bool same(int x,int y)
    {
        int t,i,j;
        for(t=0;t<6;t++)
        {
            i=t;
            for(j=0;j<6;j++)
            {
                if(f[x].a[i]!=f[y].a[j])
                    break;
                i++;
                i=i%6;
            }
            if(j>=6) return true;
        }
      for(t=5;t>=0;t--)
        {
            i=t;
            for(j=0;j<6;j++)
            {
                if(f[x].a[i]!=f[y].a[j])
                    break;
                i--;
                i=(i+6)%6;
            }
            if(j>=6) return true;
        }
      return false;
    }
    void add(int key,int x)
    {
        if(b) return ;
        int y;
        if(hash[key].x>=1)
        {
            Node *p=hash[key].next;
            for(p;p!=NULL;p=p->next)
            {
                y=p->x;
                if(same(x,y))
                {
                    b=1;
                    return ;
                }
            }
        }
        if(hash[key].x==0)
            hash[key].next=NULL;
        Node *next=new Node;
        next->x=x;
        next->next=hash[key].next;
        hash[key].next=next;
        hash[key].x++;
    
    }
    int main()
    {
        int n,i,j;
        int key;
        while(scanf("%d",&n)!=EOF)
        {
            b=0;
        //    memset(0,sizeof(hash),sizeof(hash));
            for(i=0;i<n;i++)
            {
                for(j=0;j<6;j++)
                    scanf("%d",&f[i].a[j]);
                key=(f[i].a[0]%prime+f[i].a[1]%prime+f[i].a[2]%prime+f[i].a[3]%prime+f[i].a[4]%prime+f[i].a[5]%prime)%prime;
                f[i].key=key;
                if(b) continue;
                add(key,i);
            }
            if(b)
                printf("Twin snowflakes found.
    ");
            else printf("No two snowflakes are alike.
    ");
        }
        return 0;
    }
    
                
  • 相关阅读:
    s3c6410_uboot中的代码重定位(nand->sdram)
    s3c6410_uart初始化及读写
    6410裸机开发教程下载
    s3c6410_时钟初始化
    linux device model简述
    CentOS安装flash player插件
    实例分析ELF文件动态链接
    581. Shortest Unsorted Continuous Subarray
    221. Maximal Square
    95. Unique Binary Search Trees II
  • 原文地址:https://www.cnblogs.com/zhangdashuai/p/3791555.html
Copyright © 2011-2022 走看看