Poj 3349 snowflake snow snowflakes hash

Source: Internet
Author: User

Each snowflake has six horns, each of which is represented by a number. Enter n snowflakes. If two snowflakes are equal, twin snowflakes found is output. Otherwise, no two snowflakes are alike.
Question: Hash

# Include <iostream> # include <cstdlib> using namespace STD; # define prime 14997 struct listnode {int A [6]; struct listnode * Next ;}; struct listnode table [prime]; bool isfindsame (INT key, int * B) {listnode * P = table [Key]. next; int I, j, CNT, s; while (P! = NULL) {for (S = 0; S <6; ++ S)/* enumeration comparison start point, a always 0-5, another constant change */{for (I = CNT = 0, j = s; CNT <6; ++ I, ++ J) // clockwise {If (j> 5) j = J % 6; If (p-> A [I]! = B [J]) break; else ++ CNT;} If (CNT = 6) return true;/* If the six corners are the same, returns true */for (I = CNT = 0, j = s; CNT <6; ++ I, -- j) // {If (j <0) j = J + 6; If (p-> A [I]! = B [J]) break; else ++ CNT;} If (CNT = 6) return true;} p = p-> next;} return false ;} void insert (INT key, int * B) {listnode * P = new (listnode); For (INT I = 0; I <6; ++ I) p-> A [I] = B [I]; P-> next = table [Key]. next; Table [Key]. next = P;} int main () {int B [6], n, hashval, ID, I, j; bool flag = false; memset (table, null, sizeof (table); scanf ("% d", & N); for (I = 0; I <n &&! Flag; I ++) {for (j = hashval = 0; j <6; j ++) {scanf ("% d", B + J ); hashval + = B [J];} id = hashval % prime; If (isfindsame (ID, B) Flag = true; else insert (ID, B );} if (FLAG) 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.