A simple backtracking question
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 7 Char*mahjong[]={8 "1T","2T","3T","4T","5T","6T","7T","8T","9T",9 "1S","2S","3S","4S","5S","6S","7S","8S","9S",Ten "1W","2W","3W","4W","5W","6W","7W","8W","9W", One " Dong","NAN","XI","BEI", A "ZHONG","FA","BAI" - }; - the intConvertChar*s)//mapping processing - { - for(intI=0;i< the; i++) - if(strcmp (mahjong[i],s) = =0) + returni; - return-1; + } A at intc[ the]; - - BOOLSearchintDEP) - { - for(intI=0;i< the; i++)//enumeration of the inscribed sub - - if(c[i]>=3) in { - if(dep==3)return true; toc[i]-=3; + if(Search (dep+1))return true; -c[i]+=3; the } * for(intI=0;i< the; i++)//Enumerate CIS $ if(i%9<=6&&c[i]>=1&&c[i+1]>=1&&c[i+2]>=1)Panax Notoginseng { - if(dep==3)return true; thec[i]--;c[i+1]--;c[i+2]--; + if(Search (dep+1))return true; Ac[i]++;c[i+1]++;c[i+2]++; the } + return false; - } $ $ BOOLCheck () - { - for(intI=0;i< the; i++)//Enumerate the cards the if(c[i]>=2) - {Wuyic[i]-=2; the if(Search (0))return true; -c[i]+=2; Wu } - return false; About } $ - intMain () - { - intcasen=0; A intmj[ the]; + Chars[ -]; the BOOLOK; - while(cin>>s) $ { the if(s[0]=='0') Break; theprintf"Case %d:",++Casen); themj[0]=convert (s); the for(intI=1;i< -; i++) - { inCin>>s; themj[i]=convert (s); the } Aboutok=false; the for(intI=0;i< the; i++)//enumerate the draw the { theMemset (c,0,sizeof(c)); + for(intj=0;j< -; j + +) c[mj[j]]++; - if(c[i]>=4)Continue; thec[i]++;Bayi if(check ()) the { theok=true; -printf"%s", Mahjong[i]); - } thec[i]--; the } the if(!OK) theprintf" not ready"); -printf"\ n"); the } the return 0; the}
Uva China Mahjong (Chinese Mahjong,uva 11210)