本文主要是介绍1897: 相同的雪花(哈希表入门),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1897: 相同的雪花
时间限制: 1 Sec 内存限制: 64 MB
提交: 11 解决: 7
您该题的状态:已完成
[提交][状态][讨论版]
题目描述
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.
样例输入
<span style="color:#333333"><span style="color:black">1
2
1 2 3 4 5 6
4 3 2 1 6 5</span></span>
样例输出
<span style="color:#333333"><span style="color:black">Twin snowflakes found.</span></span>
代码中包含两种方法
#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f
const int MAXN=100005;
const int MOD=5000;
int snow[MAXN][6];
vector<int>Hash[MOD];
bool isSame(int a, int b) {int i,j,k;for(i=0;i<6;i++){for(j=0;j<6;j++){if(snow[a][j]!=snow[b][(i+j)%6]){break;}}if(j==6) return true;for(j=0;j<6;j++){if(snow[a][j]!=snow[b][(i+6-j)%6]){break;}} if(j==6) return true;}return false;/*if(//顺时针方向(snow[a][0] == snow[b][i] &&snow[a][1] == snow[b][(i+1)%6] &&snow[a][2] == snow[b][(i+2)%6] &&snow[a][3] == snow[b][(i+3)%6] &&snow[a][4] == snow[b][(i+4)%6] &&snow[a][5] == snow[b][(i+5)%6])|| //逆时针方向(snow[a][0] == snow[b][(i+5)%6] &&snow[a][1] == snow[b][(i+4)%6] &&snow[a][2] == snow[b][(i+3)%6] &&snow[a][3] == snow[b][(i+2)%6] &&snow[a][4] == snow[b][(i+1)%6] &&snow[a][5] == snow[b][(i+0)%6]))return true;*/
}
int T,N,ok;
int t;
int main()
{cin>>T;
// scanf("%d",&T);while(T--){ok=0;cin>>N;for(int i=0;i<MOD;i++)//哈希表初始化 {Hash[i].clear();}for(int i=0;i<N;i++) //输入数值 {int tot=0;for(int j=0;j<6;j++)//输入过程中并且计算其中的和 {cin>>t;tot+=t;snow[i][j]=t; //将和输入相应的二维数组 }tot=tot%MOD; //取模,防止和过大 Hash[tot].push_back(i); //将相应的和的顺序输入 }for(int i=0;i<MOD;i++){if(ok)break;if(Hash[i].size()<2)continue;for(int j=0;j<Hash[i].size()-1;j++){for(int k=j+1;k<Hash[i].size();k++){if(isSame(Hash[i][j],Hash[i][k])){ok=1;break;}}}}if(ok)printf("Twin snowflakes found.\n");else printf("No two snowflakes are alike.\n");}return 0;
}
这篇关于1897: 相同的雪花(哈希表入门)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!