Mahjong Backtracking Algorithm (no Laizi)

Source: Internet
Author: User

1#include <stdio.h>2#include <iostream>3#include <sstream>4#include <cstring>5#include <vector>6 7 using namespacestd;8typedef unsignedChar byte;9 #defineArrlen (arr) sizeof (arr)/sizeof (arr[0])Ten  One bytearr[ One] = {0x01,0x01,0x01,0x02,0x02,0x03,0x03,0x03,0x02,0x04,0x04}; A bytearrindex[5] = {0}; -vector<byte>Temparr; -vector<byte>Allgroup; the  - byteSwitchtocardindex (bytecbcarddata) - { -     returnCbcarddata-1; + } -  + byteSwitchtocarddata (byteCbcardindex) A { at     returnCbcardindex +1; - } -  - voidGetweaveitem (byteDEP) - { -  in     //The difference between each of the combinations is ultimately the difference in the cards, that is, to pass the cards recursively to the final root, can guarantee the results of non-repetition -     //Traverse each index, if the index is converted into a combination, can make dep=3, not +3 restore, to redeem other combinations to     //does not distinguish between the order of composition, but the combination of all the index of the root node, enumerate all the combinations, and +3 restore know to True +     //true to output the hand is OK, pay attention to the back of the stack, that is, the buffer backtracking -  the     if(DEP = =3&& temparr.size () = =Arrlen (arr)) *     { $cout <<"can and Cards"<<Endl;Panax Notoginseng  -          for(Auto ITR = Temparr.begin (); ITR < Temparr.end (); itr++) the         { +             if(ITR = =Temparr.begin ()) A             { thecout << ( Short)*ITR; +             } -             Else if(ITR = = Temparr.begin () +1) $cout << ( Short) *itr <<","; $             Else -cout << ( Short) *itr <<" "; -             /*Allgroup.push_back ((short) *itr);*/ the         } - Wuyicout << Endl <<Endl; the     } -  Wu      for(inti =0; I < Arrlen (Arrindex); i++)   -     {   About         if(Arrindex[i] >=3)   $         {   -Arrindex[i]-=3;  - Temparr.push_back (Switchtocarddata (i)); - Temparr.push_back (Switchtocarddata (i)); A Temparr.push_back (Switchtocarddata (i)); +  theGetweaveitem (dep+1); -  $Arrindex[i] + =3;  the Temparr.pop_back (); the Temparr.pop_back (); the Temparr.pop_back (); the  -         }   in     } the  the     //The last two don't need to be judged . About      for(inti =0; I < Arrlen (Arrindex)-2; i++) the     {   the         //arrindex[4] No error the         if(I% Arrlen (Arrindex) < (Arrlen (Arrindex)-2) && Arrindex[i] >=1&& Arrindex[i +1] >=1&& Arrindex[i +2] >=1)   +         {   -Arrindex[i]--; Arrindex[i +1] --; Arrindex[i +2] --;  the Temparr.push_back (Switchtocarddata (i));BayiTemparr.push_back (Switchtocarddata (i+1)); theTemparr.push_back (Switchtocarddata (i+2)); the  -Getweaveitem (DEP +1); -  theArrindex[i] + +; Arrindex[i +1] ++; Arrindex[i +2] ++;  the Temparr.pop_back (); the Temparr.pop_back (); the Temparr.pop_back (); -  the         }   the     } the 94 } the  the voidMain () the {98      for(byteI=0; I < Arrlen (arr); i++) About     { -Arrindex[switchtocardindex (Arr[i])]++;101     }102 103     stringstr ="";104      for(bytei =0; I < Arrlen (Arrindex); i++) the     {106 StringStream stream;107stream<< ( Short) arrindex[i];108str + =" "+stream.str ();109     } the 111cout << str <<Endl; the 113      for(inti =0; I < Arrlen (Arrindex); i++)   the     {   the         if(Arrindex[i] >=2)//the card the         {  117Arrindex[i]-=2; 118 Temparr.push_back (Switchtocarddata (i));119 Temparr.push_back (Switchtocarddata (i)); - 121Getweaveitem (0);122 123Arrindex[i] + =2;124 Temparr.pop_back (); the Temparr.pop_back ();126         }127     } - 129System"Pause"); the}

Mahjong Backtracking Algorithm (no Laizi)

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.