代码改变世界

poj 3349 Snowflake Snow Snowflakes -----hash (vector)

2012-03-15 14:58  java环境变量  阅读(260)  评论(0编辑  收藏  举报
Snowflake Snow Snowflakes
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 20853   Accepted: 5449

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.


       这是一道hash的题。先尝试的是用链表写,但 总是有点错,链表运用还是有待加强。所以先用vector  写了。
       
题目大意,有n片雪花, 雪花有六条arm,分别输入每个雪花每条 arm的长度。。如果这些输入的雪花中有相同 的 输出Twin snowflakes found.  如果任意两片都是不同的输出 No two snowflakes are alike.       。。这里的相同指的是同构,即顺时针或逆时针 满足六条arm  相同就可以。(雪花可以转动)
           
 思路:   每次输入一片 ,把六条arm长度的和作为hash表的key,在表中查找 是否有同构的雪花。具体见代码。   


//Memory: 6116 KB		Time: 3500 MS
//Language: C++		Result: Accepted
#include<cstdio>
#include<vector>
using namespace std;

bool cmp(int a,int b);     //判断是否同构
const int MAX=99991;       
int arm[100050][6];
vector<int> hash[MAX];      //hash表
int main()
{
	//freopen("1.txt","r",stdin);
	int n,i,j,k;
	while(scanf("%d",&n)!=EOF)
	{
		int ok;
		for(i=0;i<MAX;i++)    //清空。
			hash[i].clear();
		for(i=0;i<n;i++)
		{
			int sum=0;
			ok=0;
			for(j=0;j<6;j++)
			{
				scanf("%d",&arm[i][j]);
				sum+=arm[i][j];
			}
			if(sum>=99991) sum%=99991;      //除余法  取余数为key
			for(k=0;k<hash[sum].size();k++)   //看hash表对应的key中是否已出现同构的雪花
				if(cmp(hash[sum][k],i))    //如果有,则完成了搜索。
				{
					ok=1;
					break;
				}
            hash[sum].push_back(i);      //如果没有,将这一片加入hash表对应的key的后边
			if(ok) break;
		}
		for(j=1;j<=(n-i-1)*6;j++)    //因为这里错了一次,边输入变查找,所以可能缓存区中还有未读的数据。要清除
			scanf("%*d");
		if(ok) printf("Twin snowflakes found.\n");
		else printf("No two snowflakes are alike.\n");
	}
	return 0;
}

bool cmp(int a,int b) 
{
	int j,k;
	for(j=0;j<6;j++)
	{
		if(arm[a][0]==arm[b][j])
		{
			for(k=1;k<6;k++)
				if(arm[a][k]!=arm[b][(j+k)%6]) break;
			if(k==6) return 1;
			for(k=1;k<6;k++)
				if(arm[a][k]!=arm[b][(j-k+6)%6]) break;
			if(k==6) return 1;
		}
	}
	return 0;
}