您的位置:首页 > 其它

17 - 01 - 04 POJ 3349 (hash)

2016-12-26 11:38 267 查看
Snowflake Snow Snowflakes

Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 39915 Accepted: 10475
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 arm
4000
s 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.


#include<stdio.h>
#include<string.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
using namespace std;
#define NN 15000 // 14997 + 3
#define NL 100
#define L 6
int m[NN];
typedef struct{
int a[L];
}item;

item snow [NN][NL];

int cmp(item x, item y);
int hash(item SNOW);

int main() {
int i, j, pos, n;
item SNOW;
memset(m, 0, sizeof(m));
scanf("%d", &n);
for (i = 1; i <= n; i++){ //读取雪花边数
for (j = 0; j < 6; j++)
{
scanf("%d", &SNOW.a[j]);
}
pos = hash(SNOW); //每读入一片雪花都:调用hash
for (j = 0; j < m[pos]; j++){
if(cmp(SNOW, snow[pos][j]) == 1)
{
puts("Twin snowflakes found.");
return 0;
}
}
snow[pos][m[pos]] = SNOW;
m[pos]++;
}
puts("No two snowflakes are alike.");
return 0;
}
int cmp(item x, item y){ //比较雪花的边数。
int st, i, j;
for (st = 0; st < 6; st++){ //顺时针
for (i = st, j = 0; j < 6; j++, i = (i + 1) % 6){
if(x.a[i] != y.a[j]) break; //跳出当前循环,st 增一。
}
if(j == 6) return 1;
}
for (st = 0; st < 6; st++){ // 逆时针
for (i = st, j = 0; j < 6; j++, i = (i + 5) % 6){
if(x.a[i] != y.a[j]) break;
}
if(j == 6) return 1;
} //顺时针或者逆时针,只要有一个满足就是相同的两片雪花
return 0;
}

int hash(item SNOW){
int i ;
int sum = 0;
for (i = 0; i < 6; i++){
sum = sum + SNOW.a[i];
} //将六片叶子相加得和
key = key%14997;
return key; //返回“和”
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: