zoukankan      html  css  js  c++  java
  • POJ 3349:Snowflake Snow Snowflakes 六片雪花找相同的 哈希

    Snowflake Snow Snowflakes
    Time Limit: 4000MS   Memory Limit: 65536K
    Total Submissions: 35642   Accepted: 9373

    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.

    题意是有很多片雪花,每一个雪花有六片,问这些雪花中是否有相同的雪花,注意雪花可以旋转,即可以循环向前移动或是向后移动,变成相等的情况。

    两两比对会TLE,哈希做,六个值的和mod一个质数 作为key,然后有 和相等 的情况下,逐一比对是否含有完全相等的情况。

    代码:

    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <string>
    #include <cstring>
    #include <map>
    #pragma warning(disable:4996)
    using namespace std;
    
    const int N1 = 15000;
    const int N2 = 100;
    int num,m[15000];
    
    struct no {
    	int sno[7];
    }node[N1][N2];
    
    bool check(no node_one,int pos, int x)
    {
    	int i, j;
    	bool flag = false;
    	for (j = 0; j < 6; j++)
    	{
    		for (i = 1; i <= 6; i++)
    		{
    			if (node_one.sno[i] != node[pos][x].sno[(i + j) % 6 + 1])
    			{
    				break;
    			}
    			if (i == 6)
    			{
    				return true;
    			}
    		}
    	}
    	
    	for (j = 7; j >= 2 ; j--)
    	{
    		for (i = 1; i <= 6; i++)
    		{
    			if (node_one.sno[i] != node[pos][x].sno[(j-i+6)%6+1])
    			{
    				break;
    			}
    			if (i == 6)
    				return true;
    		}
    	}
    	return flag;
    }
    
    int main()
    {
    
    	int i,j,pos;
    	bool flag = true;
    	no node_one;
    
    	memset(m, 0, sizeof(m));
    	scanf("%d", &num);
    
    	for (i = 1; i <= num; i++)
    	{
    		for (j = 1; j <= 6; j++)
    		{
    			scanf("%d",&node_one.sno[j]);
    		}
    		pos = (node_one.sno[1] + node_one.sno[2] + node_one.sno[3] + node_one.sno[4] + node_one.sno[5] + node_one.sno[6])%14997;
    
    		for (j = 0; j < m[pos]; j++)
    		{
    			if (check(node_one,pos,j))
    			{
    				puts("Twin snowflakes found.");
    				return 0;
    			}
    		}
    			node[pos][m[pos]] = node_one;
    			m[pos]++;
    	}
    	puts("No two snowflakes are alike.");
    	return 0;
    }


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    你不知道的多重循环的事情
    【浅墨Unity3D Shader编程】之三 光之城堡篇:子着色器、通道与标签的写法 &amp; 纹理混合
    ssh 远程登陆指定port
    GMT和CST的转换
    Swift新手教程系列5-函数+selector在swift中的使用方法
    Mac OS X 10.10, Eclipse+ADT真机调试代码时,Device Chooser中不显示真机的解决方式
    android studio 0.8.1使用和遇到问题解决
    C语言之函数调用17—递归法之中的一个般函数的调用(2)
    C语言之基本算法25—牛顿迭代法求方程近似根
    FineUI之使用SQL脚本从数据库表中生成对应的输入控件
  • 原文地址:https://www.cnblogs.com/lightspeedsmallson/p/4928117.html
Copyright © 2011-2022 走看看