POJ 3349 Snowflake Snow Snowflakes(哈希)

先哈希,再对比,对比就是循环滚动对比

C++ Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
#define MAX 100005
#define KEY 100000

using namespace std;

int snowFlakes[MAX][6];
vector<int> head[KEY];

bool isSame(int a, int b){
for(int i = 0; i < 6; i++){
bool flag = true;
for(int j = 0; j < 6; j++){
if(snowFlakes[a][j] != snowFlakes[b][(i+j)%6]){
flag = false;
break;
}
}
if(flag == true)
return true;
}
for(int i = 0; i < 6; i++){
bool flag = true;
for(int j = 0; j < 6; j++){
if(snowFlakes[a][j] != snowFlakes[b][(i-j+6)%6]){
flag = false;
break;
}
}
if(flag == true)
return true;
}
return false;
}

int main(){
int num, sum;
bool flag = false;
scanf("%d", &num);
for(int i = 0; i < num; i++){
sum = 0;
for(int j = 0; j < 6; j++){
scanf("%d", &snowFlakes[i][j]);
sum += snowFlakes[i][j];
}
if(flag == false){
int key = sum % KEY;
if(head[key].size() > 0){
for(int j = 0; j < head[key].size(); j++){
if(isSame(head[key][j], i)){
flag = true;
break;
}
}
}
head[key].push_back(i);
}
}
if(flag){
printf("Twin snowflakes found.\n");
}else{
printf("No two snowflakes are alike.\n");
}
return 0;
}