nyoj-130-相同的雪花(hash)
/* Name:NYOJ-130-相同的雪花 Copyright: Author: Date: 2018/4/14 15:13:39 Description: 将雪花各个分支上的值加起来,hash */ #include <iostream> #include <vector> #include <cstdio> #include <cstring> using namespace std; const int MAXN = 1e5+10; int snowflakes[MAXN][7]; vector<int> snow[5001];//hash int Like(int x,int y)//判断两片雪花是否相同 { for(int i=0;i<6;i++) { if((snowflakes[x][0]==snowflakes[y][i]&&snowflakes[x][1]==snowflakes[y][(i+1)%6]&&snowflakes[x][2]==snowflakes[y][(i+2)%6]&&snowflakes[x][3]==snowflakes[y][(i+3)%6]&&snowflakes[x][4]==snowflakes[y][(i+4)%6]&&snowflakes[x][5]==snowflakes[y][(i+5)%6])///顺时针方向判断两个雪花是否相同 ||(snowflakes[x][0]==snowflakes[y][(i+5)%6]&&snowflakes[x][1]==snowflakes[y][(i+4)%6]&&snowflakes[x][2]==snowflakes[y][(i+3)%6]&&snowflakes[x][3]==snowflakes[y][(i+2)%6]&&snowflakes[x][4]==snowflakes[y][(i+1)%6]&&snowflakes[x][5]==snowflakes[y][i]))///逆时针方向判断两个雪花是否相同 return 1; } return 0; } int main() { // freopen("in.txt", "r", stdin); int t; cin>>t; while (t--) { memset(snow, 0, sizeof(snow)); int n; cin>>n; for (int i=0; i<n; i++) { int sum = 0; for (int j=0; j<6; j++) { int tmp; cin>>tmp; snowflakes[i][j] = tmp; sum += tmp; } sum = sum % 5000;//防止越界 snow[sum].push_back(i); } int flag = 0 ; for (int i=0; i<5000; i++) { if (flag) break; if (snow[i].size() < 2) continue; for (int j=0; j<snow[i].size()-1; j++) { for (int k=j+1; k<snow[i].size(); k++) { if (Like(snow[i][j], snow[i][k])) { flag = 1; break; } } } } if(flag)printf("Twin snowflakes found.\n"); else printf("No two snowflakes are alike.\n"); } return 0; }