标签:nyoj130
相同的雪花
时间限制: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.
-
来源
- POJ
-
上传者
- 张云聪
题意:给定多组包含6组数的序列,判断其中是否有两组是相等的,相等的依据是这两组数正序匹配或者逆序匹配。
题解:以每组的和为key对每片雪花进行哈希,然后从和相等的雪花里进行匹配。
#include <stdio.h>
#include <string.h>
#include <vector>
#define maxn 100001
#define MOD 100001
int n;
struct Node {
int a[6];
} snow[maxn];
std::vector<int> f[MOD];
void getData() {
int i, j, sum;
memset(f, 0, sizeof(f));
scanf("%d", &n);
for(i = 0; i < n; ++i) {
for(j = sum = 0; j < 6; ++j) {
scanf("%d", &snow[i].a[j]);
sum += snow[i].a[j];
}
f[sum % MOD].push_back(i);
}
}
bool Judge(int x, int y) {
int i, j, count;
for(i = 0; i < 6; ++i) {
if(snow[x].a[i] == snow[y].a[0]) {
for(count = j = 0; j < 6; ++j)
if(snow[x].a[(i+j)%6] == snow[y].a[j])
++count;
if(count == 6) return true;
for(count = j = 0; j < 6; ++j)
if(snow[x].a[((i-j)%6+6)%6] == snow[y].a[j])
++count;
if(count == 6) return true;
}
}
return false;
}
bool test(int k, int m) {
int i, j;
for(i = 0; i < m; ++i)
for(j = i + 1; j < m; ++j)
if(Judge(f[k][i], f[k][j]))
return true;
return false;
}
void solve() {
int i, j, k, ok = 0;
for(i = 0; i < MOD; ++i)
if(!f[i].empty() && test(i, f[i].size())) {
ok = 1; break;
}
printf(ok ? "Twin snowflakes found.\n"
: "No two snowflakes are alike.\n");
}
int main() {
// freopen("stdin.txt", "r", stdin);
int t;
scanf("%d", &t);
while(t--) {
getData();
solve();
}
return 0;
}
NYOJ130 相同的雪花 【Hash】
标签:nyoj130
原文地址:http://blog.csdn.net/chang_mu/article/details/40180509