UVA-11210-Chinese (Mahjong)

Source: Internet
Author: User

UVA-11210-Chinese (Mahjong)

The 24-page training guide is really drunk. I thought it was an endless loop. Originally, there were too many loop sets. A group of samples, 500 S + recursion, would be reduced as much as possible. nested loops would have killed my complexity. 34*14*13*12*11*10*9*8*7*6*5*4*3*2*1 it's hard to use your mind !!!
Pay special attention to future backtracking. Never forget to call the function and change the variable back.

# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        Using namespace std; vector
       
         Have; set
        
          Ans; int ji = 0, kase = 0; bool zuhe (vector
         
           Now) {int flagzong = 0; for (int I = 0; I <now. size (); I ++) if (now [I]! = 99) {flagzong = 1; break;} if (! Flagzong) return true; // finally finished for (int I = 0; I <now. size (); I ++) for (int j = 0; j <now. size (); j ++) for (int k = 0; k <now. size (); k ++) {if (I = j | I = k | j = k | now [I] = 99 | now [j] = 99 | now [k] = 99) continue; int a = now [I], B = now [j], c = now [k]; int flag = 0; if (a = B & a = c) // engraved child {vector
          
            Now1 (now); now1 [I] = 99; now1 [j] = 99; now1 [k] = 99; if (zuhe (now1) return true ;} else if (a> = 0 & a <= 8 & B> = 0 & B <= 8 & c> = 0 & c <= 8) | (a> = 9 & a <= 17 & B> = 9 & B <= 17 & c> = 9 & c <= 17) | (a> = 18 & a <= 26 & B> = 18 & B <= 26 & c> = 18 & c <= 26 )) // shun sub {if (a + 1 = B & B + 1 = c) flag = 1; if (a + 1 = c & c + 1 = B) flag = 1; if (B + 1 = a & a + 1 = c) flag = 1; if (B + 1 = c & c + 1 = a) flag = 1; if (c + 1 = a & a + 1 = B) flag = 1; if (c + 1 = B & B + 1 =) flag = 1; if (flag) {vector
           
             Now1 (now); now1 [I] = 99; now1 [j] = 99; now1 [k] = 99; if (zuhe (now1) return true ;}}} return false;} bool ting (vector
            
              Now) // select two cards for {for (int I = 0; I <now. size (); I ++) for (int j = 0; j <now. size (); j ++) {if (I = j | now [I]! = Now [j]) continue; vector
             
               Temp (now); temp [I] = 99; temp [j] = 99; if (zuhe (temp) return true;} return false;} void solve () {for (int I = 0; I <34; I ++) {int countt = 0; for (int j = 0; j 
                Temp (have); temp. push_back (I); if (ting (temp) ans. insert (I) ;}} void build (string str) {if (str = "DONG") have. push_back (27); if (str = "NAN") have. push_back (28); if (str = "XI") have. push_back (29); if (str = "BEI") have. push_back (30); if (str = "ZHONG") have. push_back (31); if (str = "FA") have. push_back (32); if (str = "BAI") have. push_back (33); if (str [1] = 'T') have. push_back (str [0]-'0'-1); if (str [1] = 's') have. push_back (str [0]-'0' + 8); if (str [1] = 'W') have. push_back (str [0]-'0' + 17);} void print () {set
               
                 : Iterator it; if (ans. empty () {printf ("Not ready \ n"); return ;}for (it = ans. begin (); it! = Ans. end (); it ++) {printf (""); if (* it)> = 0 & * it <= 8) printf ("% dT ", (* it) + 1); if (* it)> = 9 & * it <= 17) printf ("% dS", (* it)-8 ); if (* it)> = 18 & * it <= 26) printf ("% dW", (* it)-17); if (* it) = 27) printf ("DONG"); if (* it) = 28) printf ("NAN"); if (* it) = 29) printf ("XI"); if (* it) = 30) printf ("BEI"); if (* it) = 31) printf ("ZHONG"); if (* it) = 32) printf ("FA"); if (* it) = 33) print F ("BAI");} printf ("\ n"); return;} int main () {string str; while (cin> str & str! = "0") {build (str); ji ++; if (ji % 13 = 0) {solve (); printf ("Case % d :", ++ kase); print (); ji = 0; have. clear (); ans. clear () ;}} return 0 ;}
               
              
             
            
           
          
         
        
       
      
     
    
   
  

The following is the code of Jia Shen:

# Include
  
   
# Include
   
    
Int mj [20], cnt [35]; const char * mahjong [] = {"1 T", "2 T", "3 T", "4 T ", "5 T", "6 T", "7 T", "8 T", "9 T", "1 S", "2 S", "3 S ", "4 S", "5 S", "6 S", "7 S", "8 S", "9 S", "1 W", "2 W ", "3 W", "4 W", "5 W", "6 W", "7 W", "8 W", "9 W", "DONG ", "NAN", "XI", "BEI", "ZHONG", "FA", "BAI"}; int getNum (char * str) {// convert the card to the ID for (int I = 0; I <34; I ++) {if (! Strcmp (mahjong [I], str) return I ;}} int check2 (int n) {for (int I = 0; I <34; I ++) {// engraved subif (cnt [I]> = 3) {if (n = 3) return 1; cnt [I]-= 3; if (check2 (n + 1) return 1; cnt [I] + = 3 ;}}for (int I = 0; I <= 24; I ++) {// subif (I % 9 <= 6 & cnt [I] & cnt [I + 1] & cnt [I + 2]) {if (n = 3) return 1; cnt [I] --; cnt [I + 1] --; cnt [I + 2] --; if (check2 (n + 1) return 1; cnt [I] ++; cnt [I + 1] ++; cn T [I + 2] ++;} return 0;} int check () {for (int I = 0; I <34; I ++) {if (cnt [I]> = 2) {// convert cnt [I]-= 2; if (check2 (0) return 1; cnt [I] + = 2;} return 0;} int main () {char str [3]; int Case = 1; while (scanf ("% s ", str) = 1) {if (strcmp (str, "0") = 0) break; printf ("Case % d:", Case ++ ); mj [0] = getNum (str); for (int I = 1; I <13; I ++) {scanf ("% s", str ); mj [I] = getNum (str); // convert the card to number} int f Lag = 0; for (int I = 0; I <34; I ++) {// enumerated 34 possible conditions: memset (cnt, 0, sizeof (cnt); for (int j = 0; j <13; j ++) {cnt [mj [j] ++; // count the number of occurrences of each card} if (cnt [I]> = 4) continue; // if the number of occurrences of each card appears four times, the card cnt [I] ++ is not considered; if (check () {flag = 1; printf ("% s", mahjong [I]) ;}} if (! Flag) printf ("Not ready/n"); else printf ("/n");} return 0 ;}
   
  

Related Article

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.