POJ-3349 Snowflake Snow Snowflakes 解题报告

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.


       题目链接:http://poj.org/problem?id=3349

       解法类型:hash

       解题思路:这题的数据很大,用hash表可以减少搜索时间。可以把每个雪花的length相加得一个sum,然后mod一个大的素数作为key值,把相应的结构存储下来,然后只要比较sum值为相同的雪花是否同构就行了。由于这里的数据可能出现很多个,冲突情况会很多,所以用开散列hash时间效率会高一点。  PS:这几天刚刚接触hash,于是这题一连就分别写了3遍,开始用的是系统类vector写的,时间效率很低。然后就自己用链表写了一个开散列hash,时间效率高了1/4的样子。但用闭散列写的代码,却是超时,可能是这题的冲突情况太多了吧。

       算法实现:

直接利用vector写的:

//STATUS:C++_AC_3844MS_6116K
#include<stdio.h>
#include<vector>
using namespace std;
bool is_same(int a,int b);
const int MAXN=99991;
int map[MAXN+30][6];
vector<int> hash[MAXN];
int main()
{
//	freopen("in.txt","r",stdin);
	int n,i,j,k,s,ok;	
	while(scanf("%d",&n)!=EOF)
	{
		ok=1;

		for(i=0;i<n;i++)
		{
			for(j=0,s=0;j<6;j++){
				scanf("%d",&map[i][j]);
				s+=map[i][j];
			}
			if(ok){      //如果已经搜索到,则不必运行
				s%=MAXN;
				for(k=0;k<hash[s].size();k++) //s值相同时的比较
					if(is_same(hash[s][k],i))
					    ok=0;
	     		hash[s].push_back(i);  //构建hash
			}
		}
		printf("%s\n",ok?"No two snowflakes are alike.":"Twin snowflakes found.");
	}
	return 0;
}

bool is_same(int a,int b)               //比较是否同构
{                                       //比如对于样例123456 和 432165
	int t[12],i,j,ok;                   //case1: 123456123456 和 432165 匹配  (不匹配)
	for(i=0;i<6;i++)                    //case2: 654321654321 和 432165 匹配  (匹配)
		t[i]=map[a][i];                 //第2种情况匹配,所以雪花同构~~
	for(i=0;i<6;i++)
		t[i+6]=map[a][i];
	for(i=0;i<6;i++){
		for(j=0,ok=1;j<6;j++){
			if(t[i+j]!=map[b][j]){ok=0;break;}
		}
		if(ok)return 1;
	}
	for(i=11;i>=6;i--){
		for(j=0,ok=1;j<6;j++){
			if(t[i-j]!=map[b][j]){ok=0;break;}
		}
		if(ok)return 1;
	}
	return 0;
}

用链表写的:
//STATUS:C++_AC_3172MS_6384K
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
const int MAXN=100010,M_PRI=99991;
bool is_same(int *a,int *b);
int map[MAXN][6];
struct NODE     //链表建立
{
	NODE() {next=NULL;};   
	int num;
	NODE *next;
}hash[M_PRI];
int main()
{
//	freopen("in.txt","r",stdin);
	int i,j,n,sum,ok;
	while(~scanf("%d",&n))
	{
		ok=1;
		memset(hash,0,sizeof(hash));

		for(i=0;i<n;i++)
		{
			for(j=0,sum=0;j<6;j++){
				scanf("%d",&map[i][j]);
				sum+=map[i][j];
			}
			if(ok){             //如果已经搜索到,则不必运行
				sum%=M_PRI;
				NODE *p=&hash[sum];
				for(;p->next!=NULL;p=p->next){         //sum值相同时,比较是否存在同构的可能
					if( is_same(*(map+p->num),*(map+i)) )ok=0;
				}
				NODE *q=(NODE*)malloc(sizeof(NODE));
				p->num=i;
				p->next=q;
				q->next=NULL;
			}
		}
    	printf("%s\n",ok?"No two snowflakes are alike.":"Twin snowflakes found.");
	}
	return 0;
}

bool is_same(int *a,int *b)     //判断雪花是否同构,顺时针和逆时针同时判断
{
	for(int i=0;i<6;i++){
		if( (a[i]==b[0] &&
			a[(1+i)%6]==b[1] &&
			a[(2+i)%6]==b[2] &&
			a[(3+i)%6]==b[3] &&
			a[(4+i)%6]==b[4] &&
			a[(5+i)%6]==b[5] )||
			(a[i]==b[5] &&
			a[(1+i)%6]==b[4] &&
			a[(2+i)%6]==b[3] &&
			a[(3+i)%6]==b[2] &&
			a[(4+i)%6]==b[1] &&
			a[(5+i)%6]==b[0] ) )return 1;
	}
	return 0;
}

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值