QUESTION:
Write A class dominochecker that have a method called Addbox (int[]) that takes a box of five dominoes, described as a list of integers (explained after), adds it-a collection, and returns True if a box with the same dominoes is already in The collection and False otherwise. A box of dominoes is encoded as a list of ten integers from 0 to 9, where a pair of numbers represent a domino. For example:0,2,9,1,3,3,7,4,5,6 represents a box containing dominoes: (0,2); (9,1); (3,3); (7,4); (5,6). Solution:
typedef pair<int,int>Domino;classDominochecker {unordered_set<Long Long>Hash; Vector< vector< Domino > >boxes; Public: BOOLAddbox (Constvector<int>&box) {Vector< Domino >v; for(inti =0; I <5; i++) {Domino D (box[2*i], box[2*i+1]); if(D.first >d.second) Swap (D.first, d.second); //order does not matterV.push_back (d); } sort (V.begin (), V.end ()); //order does not matter. Long LongHash_value =0; for(inti =0; I <5; i++) Hash_value= Hash_value * -+ v[i].first*Ten+ V[i].second;//consider an int array as an integer if(Hash.find (hash_value)! =hash.end ())return false; Hash.insert (Hash_value); Boxes.push_back (v); //I suppose we want to store them return true; }};
Look for hash values--think of an int array as an integer