相同的雪花
时间限制: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 <iostream>
#include <cstdio> using namespace std; typedef struct snow{
int a[];
snow *next;
};
snow *s[]; int fin(int b[],int sum){
snow *p;
p=s[sum]->next;
while(p){
for(int i=;i<;i++){
int j;
if(b[i]==p->a[]){
for(j=;j<;j++){
if(b[i+j]%!=p->a[j]){
break;
}
}
if(j==){
return ;
}
for(j=;j<;j++){
if(b[((i-j%+)%)]!=p->a[j]){
break;
}
}
if(j==){
return ;
}
}
}
p=p->next;
}
p=new snow;
for(int i=;i<;i++){
p->a[i]=b[i];
}
p->next=s[sum]->next;
s[sum]->next=p;
return ;
} int main()
{
int n;
int t;
int b[];
int sum;
int flag;
scanf("%d",&n);
for(int i=;i<;i++){
s[i]=new snow;
s[i]->next=NULL;
}
while(n--){
scanf("%d",&t);
flag=;
for(int i=;i<t;i++){
sum=;
for(int j=;j<;j++){
scanf("%d",&b[j]);
sum+=b[j];
}
sum%=;
if(!flag){
if(fin(b,sum)){
flag=;
}
}
}
if(flag){
printf("Twin snowflakes found.\n");
}else{
printf("No two snowflakes are alike.\n");
}
} return ;
}