Snowflake Snow Snowflakes
Time Limit:4000MS Memory Limit:65536KB
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.
题意:
在n (n<100000)个雪花中判断是否存在两片完全相同的雪花,每片雪花有6个角,每个角的长度限制为1000000
两片雪花相等的条件:
雪花6个角的长度按顺序相等(这个顺序即可以是顺时针的也可以是逆时针的)
解题思路:
哈希表
设雪花6片叶子的长度为len1~len6
key =( len1%prime +len2%prime +len3%prime +len4%prime +len5%prime +len6)%prime
//Memory 6452 KB Time 3344 ms
#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
#define pr 9773
#define ll __int64
struct snowflake
{
ll leaf[6];
};
bool comp(snowflake a,snowflake b)//两片雪花正向,逆向比较
{
int i,j;
for(i=0;i<6;i++)
{
for(j=0;j<6;j++)
if(a.leaf[j]!=b.leaf[(j+i)%6]) break;
if(j==6) return true;
for(j=0;j<6;j++)
if(a.leaf[5-j]!=b.leaf[(j+i)%6]) break;
if(j==6) return true;
}
return false;
}
int main()
{
int i,j,n,x,k;
snowflake s;
while(scanf("%d",&n)!=EOF)
{
k=0;//k=1表示已找到相同的
vector<snowflake> vec[pr+1];//vector容器保存key值相同的雪花
for(i=1;i<=n;i++)
{
scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&s.leaf[0],&s.leaf[1],&s.leaf[2],&s.leaf[3],&s.leaf[4],&s.leaf[5]);
x=(s.leaf[0]%pr+s.leaf[1]%pr+s.leaf[2]%pr+s.leaf[3]%pr+s.leaf[4]%pr+s.leaf[5]%pr)%pr;
if(!k){
for(j=0;j<vec[x].size();j++)
if(comp(vec[x][j],s)){//key值相同的相比较
k=1;break;
}
if(!k) vec[x].push_back(s);
}
}
if(k) printf("Twin snowflakes found.\n");
else printf("No two snowflakes are alike.\n");
}
return 0;
}