码迷,mamicode.com
首页 > 其他好文 > 详细

相同的雪花

时间:2015-07-19 13:26:46      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

相同的雪花

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
描述
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.
输入
The first line of the input will contain a single interger T(0<T<10),the number of the test cases.
The first line of every test case 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.
输出
For each test case,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.
样例输入
1
2
1 2 3 4 5 6
4 3 2 1 6 5
样例输出
Twin snowflakes found.

代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define Max_size 1000000
#define Key 999983
struct node
{
	int v[6];
}s[Max_size];
int found(int m)
{
	return m%Key;
}
bool equals(node *a,node *b)
{
	int p,q,i,j;
	for(i=0;i<6;i++)
	{
		for(j=0;j<6;j++)
		{
			if(a->v[i]==b->v[j])
			{
				int count=0;
				p=i;
				q=j;
				while(a->v[p]==b->v[q]&&count<5)
				{
					p=(p+1)%6;q=(q+1)%6;
					count++;
				}
				if(count==5)
				{
					return true;
				}
				count=0;
				p=i;
				q=j;
				while(a->v[p]==b->v[q]&&count<5)
				{
					p=(p+1)%6;
					q=(q==-1?5:q-1);
					count++;
				}
				if(count==5)
				{
					return true;
				}
			}
		}
	}
	return false;
}
bool found_hash(node t,int sum)
{
	for(int k=found(sum);;k++)
	{
		if(k==Key)
		{
			k=k%Key;
		}
		if(s[k].v[0]==-1)
		{
			s[k]=t;
			return false;
		}
		if(equals(&s[k],&t))
		{
			return true;
		}
	}
	return false;
}
int main(void)
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		node m; 
		bool flag=false;
		int sum,n,i,j;
		memset(s,-1,sizeof(s));
		scanf("%d",&n);
		for(i=0;i<n;i++)
		{
			sum=0;
			for(j=0;j<6;j++)
			{
				scanf("%d",&m.v[j]);
				sum=sum+m.v[j];
			}
			if(!flag)
			{
				flag=found_hash(m,sum);
			}
		}
		if(flag)
			printf("Twin snowflakes found.\n");
		else 
		    printf("No two snowflakes are alike.\n");		
	}
	return  0;
}


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

相同的雪花

标签:

原文地址:http://blog.csdn.net/qq_16997551/article/details/46953947

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!