一、题目内容
POJ 3349 原题地址
二、题意解释
在n(n<100000)个雪花中判断是否存在两片完全相同的雪花,每片雪花有6个角,每个角的长度限制为1000000
两片雪花相等的条件:
雪花6个角的长度按顺序相等(这个顺序即可以是顺时针的也可以是逆时针的)
三、代码及注释
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct node
{
int side[6];
};
node hash_[15000][1000];
int top[15000];
int getKey(node& demo)
{
long long key = 0;
for (int i = 0; i < 6; i++)
key += demo.side[i];
key %= 14997;
return (int)key;
}
bool cmp(node first, node second)
{
sort(first.side, first.side+6);
sort(second.side, second.side+6);
for (int i = 0; i < 6; i++)
{
if (first.side[i] != second.side[i])
return false;
}
return true;
}
int main()
{
int n, tmpKey;
node tmp;
scanf("%d", &n);
memset(top, 0, sizeof(top));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 6; j++)
{
scanf("%d", &tmp.side[j]);
}
tmpKey = getKey(tmp);
for (int j = 0; j < top[tmpKey]; j++)
{
if (cmp(hash_[tmpKey][j], tmp))
{
printf("Twin snowflakes found.\n");
return 0;
}
}
hash_[tmpKey][top[tmpKey]++] = tmp;
}
printf("No two snowflakes are alike.\n");
return 0;
}