nyoj 130 相同的雪花
阿新 • • 發佈:2018-12-24
用雜湊表直接新增,查詢就行了
#include<stdio.h> #include<iostream> #include<cstring> #include<vector> using namespace std; #define MOD 100001 //key 大質數 #define N 100001 vector<int> f[MOD]; int snow[N][6]; void Input(int i) { int sum = 0; for(int j = 0; j < 6; j++) { scanf("%d",&snow[i][j]); sum += snow[i][j]; } f[sum%MOD].push_back(i); } bool IsValid(int m,int a,int b) { int i,j,k,flag = 0; for(i = 0; i < 6; i++) { if(snow[f[m][a]][i] == snow[f[m][b]][0]) { int count = 1; for(j = 1; j < 6; j++) { if(snow[f[m][a]][(i+j)%6] == snow[f[m][b]][j]) count++; } if(count == 6) {flag = 1;break;} count = 1; for(j = 1; j < 6; j++) { if(snow[f[m][a]][(i-j+6)%6] == snow[f[m][b]][j]) count++; } if(count == 6) {flag = 1;break;} } } return flag ; } bool Test(int i) { int j,k,flag = 0; for(j = 0; j< f[i].size()-1; j++) { for(k = j+1; k < f[i].size(); k++) if(IsValid(i,j,k)) { flag = 1; return flag ; } } return flag ; } int main() { int n,i,flag,test; scanf("%d",&test); while(test--) { flag=0; memset(f,0,sizeof(f)); scanf("%d",&n); for(i=0; i<n; i++) Input(i); for(i = 0; i < MOD; i++) { if(f[i].size() > 1) { if(Test(i)) flag = 1; } } if(flag) printf("Twin snowflakes found.\n"); else printf("No two snowflakes are alike.\n"); } return 0; }