Poj 3349 snowflake snow snowflakes (hash table)

Source: Internet
Author: User

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 ;}



 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.