Each snowflake has six petals, N slices of snowflake, and the length of the six petals is clockwise or counterclockwise. Check if there are any identical snowflakes (the length of the six petals is the same)
Thought: If the traversal times out, I tried it. Here we need to use a hash table. The key code key of the hash table is obtained using the length of the six petals and the remainder of the number. The address is the storage location of the snowflake (that is, the location in the snowflakes array)
Code:
# Include <iostream> # include <vector> using namespace STD; const int maxn = 100000 + 100; // The maximum number of snowflakes const int mo = 98765; // Number of hash remainder int snowflakes [maxn] [6]; // stores snowflake information vector <int> hash [Mo]; // hash table bool issame (int, int B) {for (Int J = 0; j <6; j ++) {If (/* clockwise */(snowflakes [a] [0] = snowflakes [B] [J] & snowflakes [a] [1] = snowflakes [B] [(j + 1) % 6] & snowflakes [a] [2] = snowflakes [B] [(j + 2) % 6] & snowflakes [a] [3] = snowflakes [B] [( J + 3) % 6] & snowflakes [a] [4] = snowflakes [B] [(j + 4) % 6] & snowflakes [a] [5] = snowflakes [B] [(j + 5) % 6]) |/* */(snowflakes [a] [0] = snowflakes [B] [J] & snowflakes [a] [1] = snowflakes [B] [(j + 5) % 6] & snowflakes [a] [2] = snowflakes [B] [(j + 4) % 6] & snowflakes [a] [3] = snowflakes [B] [(j + 3) % 6] & snowflakes [a] [4] = snowflakes [B] [(j + 2) % 6] & snowflakes [a] [5] = snowflakes [B] [(j + 1) % 6]) return true;} re Turn false;} int main () {int N; bool exist = false; while (scanf ("% d", & N )! = EOF) {int I, j, k; for (I = 0; I <n; I ++) for (j = 0; j <6; j ++) scanf ("% d", & snowflakes [I] [J]); int sum, key; for (I = 0; I <n; I ++) {sum = 0; For (j = 0; j <6; j ++) sum + = snowflakes [I] [J]; Key = sum % Mo; // keyvector used as the hash table <int>: iterator it; for (IT = hash [Key]. begin (); it! = Hash [Key]. end (); It ++) // traverse the snowflake with the same key in the hash table if (issame (* It, I) {exist = true; break;} hash [Key]. push_back (I);} If (exist) printf ("twin snowflakes found. \ n "); elseprintf (" no two snowflakes are alike. \ n ") ;}return 0 ;}