2019年4月17日更新:

将搜索主函数优化为局部记忆化搜索,再次提高若干倍搜索速度

更新了main和player,helper无更新

  1. #include "Player-v3.0.cpp"
  2. #include "Helper.cpp"
  3.  
  4. #define END {if(fir){newA=a; newB=b;} return 1;}
  5. #define NXT {if(ok&&play(b,a,0)==0) return 1; else return 0;}
  6. #define NO {printf("Can't Out\n"); newA=a; newB=b;}
  7. player newA,newB;
  8. bool play(player,player,bool);
  9.  
  10. bool Play_Rocket(player a,player b,bool fir){
  11. if(b.empty()) return ;
  12. if(a.CanPlayRocket()){
  13. a.PlayRocket();
  14. bool now=play(a,b,);
  15. if(now){
  16. if(fir) Out_Rocket();
  17. END;
  18. }
  19. }
  20. return ;
  21. }
  22.  
  23. bool Play_Boom(player a,player b,int last,bool fir,bool ok){
  24. if(b.empty()) return ;
  25. if(Play_Rocket(a,b,fir)) return ;
  26. player _a=a;
  27. if(a.CanBoom)
  28. for(int i=last+;i<;i++)
  29. if(a.CanPlayBoom(i)){
  30. a.PlayBoom(i);
  31. a.ResetCan();
  32. bool now=Play_Boom(b,a,i,,);
  33. if(!now){
  34. if(fir) Out_Boom(i);
  35. END;
  36. }
  37. a=_a;
  38. }
  39. NXT;
  40. }
  41.  
  42. bool Play_Single(player a,player b,int last,bool fir,bool ok){
  43. if(b.empty()) return ;
  44. player _a=a;
  45. if(a.CanBoom) if(Play_Boom(a,b,-,fir,)) return ;
  46. for(int i=last+;i<;i++)
  47. if(a.CanPlaySingle(i)){
  48. a.PlaySingle(i);
  49. bool now=Play_Single(b,a,i,,);
  50. if(!now){
  51. if(fir) Out_Single(i);
  52. END;
  53. }
  54. a=_a;
  55. }
  56. NXT;
  57. }
  58.  
  59. bool Play_Couple(player a,player b,int last,bool fir,bool ok){
  60. if(b.empty()) return ;
  61. player _a=a;
  62. if(a.CanBoom) if(Play_Boom(a,b,-,fir,)) return ;
  63. for(int i=last+;i<;i++)
  64. if(a.CanPlayCouple(i)){
  65. a.PlayCouple(i);
  66. bool now=Play_Couple(b,a,i,,);
  67. if(!now){
  68. if(fir) Out_Couple(i);
  69. END;
  70. }
  71. a=_a;
  72. }
  73. NXT;
  74. }
  75.  
  76. bool Play_Three(player a,player b,int last,bool fir,bool ok){
  77. if(b.empty()) return ;
  78. player _a=a;
  79. if(a.CanBoom) if(Play_Boom(a,b,-,fir,)) return ;
  80. if(a.CanThree)
  81. for(int i=last+;i<;i++)
  82. if(a.CanPlayThree(i)){
  83. a.PlayThree(i);
  84. a.ResetCan();
  85. bool now=Play_Three(b,a,i,,);
  86. if(!now){
  87. if(fir) Out_Three(i);
  88. END;
  89. }
  90. a=_a;
  91. }
  92. NXT;
  93. }
  94.  
  95. bool Play_3Single(player a,player b,int last,bool fir,bool ok){
  96. if(b.empty()) return ;
  97. player _a=a;
  98. if(a.CanBoom) if(Play_Boom(a,b,-,fir,)) return ;
  99. for(int i=last+;i<;i++) if(a.CanPlayThree(i))
  100. if(a.CanThree)
  101. for(int j=;j<;j++)
  102. if(a.CanPlay3Single(i,j)){
  103. a.Play3Single(i,j);
  104. a.ResetCan();
  105. bool now=Play_3Single(b,a,i,,);
  106. if(!now){
  107. if(fir) Out_3Single(i,j);
  108. END;
  109. }
  110. a=_a;
  111. }
  112. NXT;
  113. }
  114.  
  115. bool Play_3Couple(player a,player b,int last,bool fir,bool ok){
  116. if(b.empty()) return ;
  117. player _a=a;
  118. if(a.CanBoom) if(Play_Boom(a,b,-,fir,)) return ;
  119. for(int i=last+;i<;i++) if(a.CanPlayThree(i))
  120. if(a.CanThree)
  121. for(int j=;j<;j++)
  122. if(a.CanPlay3Couple(i,j)){
  123. a.Play3Couple(i,j);
  124. a.ResetCan();
  125. bool now=Play_3Couple(b,a,i,,);
  126. if(!now){
  127. if(fir) Out_3Couple(i,j);
  128. END;
  129. }
  130. a=_a;
  131. }
  132. NXT;
  133. }
  134.  
  135. bool Play_4Single(player a,player b,int last,bool fir,bool ok){
  136. if(b.empty()) return ;
  137. player _a=a;
  138. if(a.CanBoom) if(Play_Boom(a,b,-,fir,)) return ;
  139. for(int i=last+;i<;i++) if(a.CanPlayBoom(i))
  140. if(a.CanBoom)
  141. for(int j=;j<;j++)
  142. for(int k=;k<;k++)
  143. if(a.CanPlay4Single(i,j,k)){
  144. a.Play4Single(i,j,k);
  145. a.ResetCan();
  146. bool now=Play_4Single(b,a,i,,);
  147. if(!now){
  148. if(fir) Out_4Single(i,j,k);
  149. END;
  150. }
  151. a=_a;
  152. }
  153. NXT;
  154. }
  155.  
  156. bool Play_4Couple(player a,player b,int last,bool fir,bool ok){
  157. if(b.empty()) return ;
  158. player _a=a;
  159. if(a.CanBoom) if(Play_Boom(a,b,-,fir,)) return ;
  160. for(int i=last+;i<;i++) if(a.CanPlayBoom(i))
  161. if(a.CanBoom)
  162. for(int j=;j<;j++)
  163. for(int k=;k<;k++)
  164. if(a.CanPlay4Couple(i,j,k)){
  165. a.Play4Couple(i,j,k);
  166. a.ResetCan();
  167. bool now=Play_4Couple(b,a,i,,);
  168. if(!now){
  169. if(fir) Out_4Couple(i,j,k);
  170. END;
  171. }
  172. a=_a;
  173. }
  174. NXT;
  175. }
  176.  
  177. bool Play_MS(player a,player b,int l,int r,bool fir,bool ok){
  178. if(b.empty()) return ;
  179. player _a=a; int pls=r-l;
  180. if(a.CanBoom) if(Play_Boom(a,b,-,fir,)) return ;
  181. if(a.CanMS)
  182. for(int i=l+;i+pls<=;i++)
  183. if(a.CanPlayMoreSingle(i,i+pls)){
  184. a.PlayMoreSingle(i,i+pls);
  185. a.ResetCan();
  186. bool now=Play_MS(b,a,i,i+pls,,);
  187. if(!now){
  188. if(fir) Out_MS(i,i+pls);
  189. END;
  190. }
  191. a=_a;
  192. }
  193. NXT;
  194. }
  195.  
  196. bool Play_MC(player a,player b,int l,int r,bool fir,bool ok){
  197. if(b.empty()) return ;
  198. player _a=a; int pls=r-l;
  199. if(a.CanBoom) if(Play_Boom(a,b,-,fir,)) return ;
  200. if(a.CanMC)
  201. for(int i=l+;i+pls<=;i++)
  202. if(a.CanPlayMoreCouple(i,i+pls)){
  203. a.PlayMoreCouple(i,i+pls);
  204. a.ResetCan();
  205. bool now=Play_MC(b,a,i,i+pls,,);
  206. if(!now){
  207. if(fir) Out_MC(i,i+pls);
  208. END;
  209. }
  210. a=_a;
  211. }
  212. NXT;
  213. }
  214.  
  215. map<pair<long long,long long>,int> Visit;
  216. bool play(player a,player b,bool fir=){
  217. if(b.empty()) return Visit[make_pair(a.Hash(),b.Hash())]=;
  218. if(a.cnt+b.cnt<&&fir==){
  219. if(Visit.count(make_pair(a.Hash(),b.Hash())))
  220. return Visit[make_pair(a.Hash(),b.Hash())];
  221. }
  222. if(a.CanBoom) if(Play_Boom(a,b,-,fir,)) return Visit[make_pair(a.Hash(),b.Hash())]=;
  223. if(a.CanMS)
  224. for(int i=;i>=;i--){
  225. if(Play_MS(a,b,-,-+i,fir,)) return Visit[make_pair(a.Hash(),b.Hash())]=;
  226. }
  227. if(a.CanMC)
  228. for(int i=;i>=;i--){
  229. if(Play_MC(a,b,-,-+i,fir,)) return Visit[make_pair(a.Hash(),b.Hash())]=;
  230. }
  231. if(a.CanThree) if(Play_Three(a,b,-,fir,)) return Visit[make_pair(a.Hash(),b.Hash())]=;
  232. if(a.CanThree) if(Play_3Single(a,b,-,fir,)) return Visit[make_pair(a.Hash(),b.Hash())]=;
  233. if(a.CanThree) if(Play_3Couple(a,b,-,fir,)) return Visit[make_pair(a.Hash(),b.Hash())]=;
  234. if(Play_Couple(a,b,-,fir,)) return Visit[make_pair(a.Hash(),b.Hash())]=;
  235. if(Play_Single(a,b,-,fir,)) return Visit[make_pair(a.Hash(),b.Hash())]=;
  236. if(a.CanBoom) if(Play_4Single(a,b,-,fir,)) return Visit[make_pair(a.Hash(),b.Hash())]=;
  237. if(a.CanBoom) if(Play_4Couple(a,b,-,fir,)) return Visit[make_pair(a.Hash(),b.Hash())]=;
  238. return Visit[make_pair(a.Hash(),b.Hash())]=;
  239. }
  240.  
  241. int Main(){
  242. Visit.clear();
  243. player a,b;
  244. printf("INPUT AI:\n");
  245. b.read();
  246. printf("INPUT PLAYER:\n");
  247. a.read();
  248. int staT=clock();
  249. if(!play(a,b,)){
  250. printf("DIE\n");
  251. return ;
  252. }
  253. printf("\nFirst Time Use %dms\n",clock()-staT);
  254. while(){
  255. a=newA; b=newB;
  256. printf("\n");
  257. if(a.empty()){
  258. printf("WIN\n");
  259. return ;
  260. }
  261.  
  262. string now; cin>>now;
  263. if(now=="EXIT") return ;
  264. if(Is_Empty(now)){
  265. if(!play(a,b,)){
  266. printf("DIE\n");
  267. }
  268. continue;
  269. }
  270. for(int i=;i<now.length();i++) b.PlaySingle(GetVal(now[i]));
  271.  
  272. if(Is_Single(now)&&Play_Single(a,b,GetVal(now[]),,)==) NO;
  273. if(Is_Couple(now)&&Play_Couple(a,b,GetVal(now[]),,)==) NO;
  274. if(Is_Three(now)&&Play_Three(a,b,GetVal(now[]),,)==) NO;
  275. if(Is_Boom(now)&&Play_Boom(a,b,GetVal(now[]),,)==) NO;
  276. if(Is_Rocket(now)&&Play_Rocket(a,b,)==) NO;
  277. if(Is_3Single(now)&&Play_3Single(a,b,GetVal(now[]),,)==) NO;
  278. if(Is_3Couple(now)&&Play_3Couple(a,b,GetVal(now[]),,)==) NO;
  279. if(Is_4Single(now)&&Play_4Single(a,b,GetVal(now[]),,)==) NO;
  280. if(Is_4Couple(now)&&Play_4Couple(a,b,GetVal(now[]),,)==) NO;
  281. if(Is_MS(now)&&Play_MS(a,b,GetVal(now[]),GetVal(now[now.length()-]),,)==) NO;
  282. if(Is_MC(now)&&Play_MC(a,b,GetVal(now[]),GetVal(now[now.length()-]),,)==) NO;
  283. }
  284. }
  285.  
  286. int main(){
  287. while() Main();
  288. }

main-v3.0

  1. /*
  2. ???????????????????
  3. ??
  4. ??????
  5. */
  6.  
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9.  
  10. int GetVal(char c){
  11. if(c=='T') return ;
  12. if(c=='J') return ;
  13. if(c=='Q') return ;
  14. if(c=='K') return ;
  15. if(c=='A') return ;
  16. if(c=='') return ;
  17. if(c=='w') return ;
  18. if(c=='W') return ;
  19. if(''<=c&&c<='') return c-'';
  20. assert();
  21. }
  22. char SetChar(int x){
  23. if(x<=) return ''+x;
  24. if(x==) return 'T';
  25. if(x==) return 'J';
  26. if(x==) return 'Q';
  27. if(x==) return 'K';
  28. if(x==) return 'A';
  29. if(x==) return '';
  30. if(x==) return 'w';
  31. if(x==) return 'W';
  32. assert();
  33. }
  34.  
  35. struct player{
  36. int a[],cnt;
  37. bool CanThree,CanBoom,CanMS,CanMC;
  38. player(){memset(a,,sizeof(a));cnt=;}
  39.  
  40. void out(){//Output Poker
  41. for(int i=;i<;i++)
  42. for(int j=;j<a[i];j++)
  43. putchar(SetChar(i));
  44. putchar('\n');
  45. }
  46.  
  47. inline bool CanPlaySingle(int x){return a[x]>=;}//?
  48.  
  49. inline bool CanPlayCouple(int x){return a[x]>=;}//?
  50.  
  51. inline bool CanPlayThree(int x){return a[x]>=;}//??
  52.  
  53. inline bool CanPlayBoom(int x){return a[x]>=;}//??
  54.  
  55. inline bool CanPlayRocket(){return a[]&&a[];}//??
  56.  
  57. inline bool CanPlayMoreSingle(int l,int r){//??
  58. if(r-l+<||r>=) return ;
  59. for(int i=l;i<=r;i++) if(!CanPlaySingle(i)) return ;
  60. return ;
  61. }
  62.  
  63. inline bool CanPlayMoreCouple(int l,int r){//??
  64. if(r-l+<||r>=) return ;
  65. for(int i=l;i<=r;i++) if(!CanPlayCouple(i)) return ;
  66. return ;
  67. }
  68.  
  69. inline bool CanPlayMoreThree(int l,int r){//??
  70. if(r-l+<||r>=) return ;
  71. for(int i=l;i<=r;i++) if(!CanPlayThree(i)) return ;
  72. return ;
  73. }
  74.  
  75. inline bool CanPlay4Single(int x,int l,int r){//???
  76. if(x==l||x==r) return ;
  77. if(l==r) return CanPlayBoom(x)&&CanPlayCouple(l);
  78. return CanPlayBoom(x)&&CanPlaySingle(l)&&CanPlaySingle(r);
  79. }
  80.  
  81. inline bool CanPlay4Couple(int x,int l,int r){//???
  82. if(x==l||x==r) return ;
  83. if(l==r) return CanPlayBoom(x)&&CanPlayBoom(l);
  84. return CanPlayBoom(x)&&CanPlayCouple(l)&&CanPlayCouple(r);
  85. }
  86.  
  87. inline bool CanPlay3Single(int x,int y){
  88. if(x==y) return ;
  89. return CanPlayThree(x)&&CanPlaySingle(y);
  90. }
  91.  
  92. inline bool CanPlay3Couple(int x,int y){
  93. if(x==y) return ;
  94. return CanPlayThree(x)&&CanPlayCouple(y);
  95. }
  96.  
  97. inline void PlaySingle(int x){a[x]--; cnt--;}
  98.  
  99. inline void PlayCouple(int x){a[x]-=; cnt-=;}
  100.  
  101. inline void PlayThree(int x){a[x]-=; cnt-=;}
  102.  
  103. inline void PlayBoom(int x){a[x]-=; cnt-=;}
  104.  
  105. inline void PlayRocket(){a[]--;a[]--; cnt-=;}
  106.  
  107. inline void PlayMoreSingle(int l,int r){for(int i=l;i<=r;i++) PlaySingle(i);}
  108.  
  109. inline void PlayMoreCouple(int l,int r){for(int i=l;i<=r;i++) PlayCouple(i);}
  110.  
  111. inline void PlayMoreThree(int l,int r){for(int i=l;i<=r;i++) PlayThree(i);}
  112.  
  113. inline void Play4Single(int x,int l,int r){PlayBoom(x); PlaySingle(l); PlaySingle(r);}
  114.  
  115. inline void Play4Couple(int x,int l,int r){PlayBoom(x); PlayCouple(l); PlayCouple(r);}
  116.  
  117. inline void Play3Single(int x,int y){PlayThree(x); PlaySingle(y);}
  118.  
  119. inline void Play3Couple(int x,int y){PlayThree(x); PlayCouple(y);}
  120.  
  121. inline bool empty(){
  122. return cnt==;
  123. }//??????
  124.  
  125. inline long long Hash(){long long res=; for(int i=;i<;i++) res=res<<|a[i]; return res;}//???
  126.  
  127. inline int GetMin(){for(int i=;i<;i++) if(a[i]) return i; assert();}
  128. inline int GetMax(){for(int i=;~i;i--) if(a[i]) return i; return -;}
  129.  
  130. inline void ResetCan(){
  131. CanThree=CanBoom=CanMS=CanMC=;//!!!
  132. for(int i=;i<;i++){
  133. if(CanPlayThree(i)) CanThree=;
  134. if(CanPlayBoom(i)) CanBoom=;
  135. }
  136. if(CanPlayRocket()) CanBoom=;
  137. for(int i=;i<=;i++){
  138. if(CanPlayMoreSingle(i,i+)) {CanMS=; break;}
  139. }
  140. for(int i=;i<=;i++){
  141. if(CanPlayMoreCouple(i,i+)) {CanMC=; break;}
  142. }
  143. }
  144.  
  145. void read(){//Input Poker
  146. memset(a,,sizeof(a)); cnt=;
  147. string s; cin>>s; int len=s.length(); cnt=len;
  148. for(int i=;i<len;i++)
  149. a[GetVal(s[i])]++;
  150. ResetCan();
  151. }
  152. };

Player-v3.0

3.0版本在双方牌数不大于15张时,基本可以在1s内出解

(OFast编译,CPU:I5-4210M,辣鸡CPU)

2019年4月16日更新:

优化了分支判断速度,快了若干倍

更新了main和player,helper无更新

  1. #include "Player-v2.0.cpp"
  2. #include "Helper.cpp"
  3.  
  4. #define END {if(fir){newA=a; newB=b;} return 1;}
  5. #define NXT {if(ok&&play(b,a,0)==0) return 1; else return 0;}
  6. #define NO {printf("Can't Out\n"); newA=a; newB=b;}
  7. player newA,newB;
  8. bool play(player,player,bool);
  9.  
  10. bool Play_Rocket(player a,player b,bool fir){
  11. if(b.empty()) return ;
  12. if(a.CanPlayRocket()){
  13. a.PlayRocket();
  14. bool now=play(a,b,);
  15. if(now){
  16. if(fir) Out_Rocket();
  17. END;
  18. }
  19. }
  20. return ;
  21. }
  22.  
  23. bool Play_Boom(player a,player b,int last,bool fir,bool ok){
  24. if(b.empty()) return ;
  25. if(Play_Rocket(a,b,fir)) return ;
  26. player _a=a;
  27. if(a.CanBoom)
  28. for(int i=last+;i<;i++)
  29. if(a.CanPlayBoom(i)){
  30. a.PlayBoom(i);
  31. a.ResetCan();
  32. bool now=Play_Boom(b,a,i,,);
  33. if(!now){
  34. if(fir) Out_Boom(i);
  35. END;
  36. }
  37. a=_a;
  38. }
  39. NXT;
  40. }
  41.  
  42. bool Play_Single(player a,player b,int last,bool fir,bool ok){
  43. if(b.empty()) return ;
  44. player _a=a;
  45. if(a.CanBoom) if(Play_Boom(a,b,-,fir,)) return ;
  46. for(int i=last+;i<;i++)
  47. if(a.CanPlaySingle(i)){
  48. a.PlaySingle(i);
  49. bool now=Play_Single(b,a,i,,);
  50. if(!now){
  51. if(fir) Out_Single(i);
  52. END;
  53. }
  54. a=_a;
  55. }
  56. NXT;
  57. }
  58.  
  59. bool Play_Couple(player a,player b,int last,bool fir,bool ok){
  60. if(b.empty()) return ;
  61. player _a=a;
  62. if(a.CanBoom) if(Play_Boom(a,b,-,fir,)) return ;
  63. for(int i=last+;i<;i++)
  64. if(a.CanPlayCouple(i)){
  65. a.PlayCouple(i);
  66. bool now=Play_Couple(b,a,i,,);
  67. if(!now){
  68. if(fir) Out_Couple(i);
  69. END;
  70. }
  71. a=_a;
  72. }
  73. NXT;
  74. }
  75.  
  76. bool Play_Three(player a,player b,int last,bool fir,bool ok){
  77. if(b.empty()) return ;
  78. player _a=a;
  79. if(a.CanBoom) if(Play_Boom(a,b,-,fir,)) return ;
  80. if(a.CanThree)
  81. for(int i=last+;i<;i++)
  82. if(a.CanPlayThree(i)){
  83. a.PlayThree(i);
  84. a.ResetCan();
  85. bool now=Play_Three(b,a,i,,);
  86. if(!now){
  87. if(fir) Out_Three(i);
  88. END;
  89. }
  90. a=_a;
  91. }
  92. NXT;
  93. }
  94.  
  95. bool Play_3Single(player a,player b,int last,bool fir,bool ok){
  96. if(b.empty()) return ;
  97. player _a=a;
  98. if(a.CanBoom) if(Play_Boom(a,b,-,fir,)) return ;
  99. for(int i=last+;i<;i++) if(a.CanPlayThree(i))
  100. if(a.CanThree)
  101. for(int j=;j<;j++)
  102. if(a.CanPlay3Single(i,j)){
  103. a.Play3Single(i,j);
  104. a.ResetCan();
  105. bool now=Play_3Single(b,a,i,,);
  106. if(!now){
  107. if(fir) Out_3Single(i,j);
  108. END;
  109. }
  110. a=_a;
  111. }
  112. NXT;
  113. }
  114.  
  115. bool Play_3Couple(player a,player b,int last,bool fir,bool ok){
  116. if(b.empty()) return ;
  117. player _a=a;
  118. if(a.CanBoom) if(Play_Boom(a,b,-,fir,)) return ;
  119. for(int i=last+;i<;i++) if(a.CanPlayThree(i))
  120. if(a.CanThree)
  121. for(int j=;j<;j++)
  122. if(a.CanPlay3Couple(i,j)){
  123. a.Play3Couple(i,j);
  124. a.ResetCan();
  125. bool now=Play_3Couple(b,a,i,,);
  126. if(!now){
  127. if(fir) Out_3Couple(i,j);
  128. END;
  129. }
  130. a=_a;
  131. }
  132. NXT;
  133. }
  134.  
  135. bool Play_4Single(player a,player b,int last,bool fir,bool ok){
  136. if(b.empty()) return ;
  137. player _a=a;
  138. if(a.CanBoom) if(Play_Boom(a,b,-,fir,)) return ;
  139. for(int i=last+;i<;i++) if(a.CanPlayBoom(i))
  140. if(a.CanBoom)
  141. for(int j=;j<;j++)
  142. for(int k=;k<;k++)
  143. if(a.CanPlay4Single(i,j,k)){
  144. a.Play4Single(i,j,k);
  145. // a.ResetCan();
  146. bool now=Play_4Single(b,a,i,,);
  147. if(!now){
  148. if(fir) Out_4Single(i,j,k);
  149. END;
  150. }
  151. a=_a;
  152. }
  153. NXT;
  154. }
  155.  
  156. bool Play_4Couple(player a,player b,int last,bool fir,bool ok){
  157. if(b.empty()) return ;
  158. player _a=a;
  159. if(a.CanBoom) if(Play_Boom(a,b,-,fir,)) return ;
  160. for(int i=last+;i<;i++) if(a.CanPlayBoom(i))
  161. if(a.CanBoom)
  162. for(int j=;j<;j++)
  163. for(int k=;k<;k++)
  164. if(a.CanPlay4Couple(i,j,k)){
  165. a.Play4Couple(i,j,k);
  166. a.ResetCan();
  167. bool now=Play_4Couple(b,a,i,,);
  168. if(!now){
  169. if(fir) Out_4Couple(i,j,k);
  170. END;
  171. }
  172. a=_a;
  173. }
  174. NXT;
  175. }
  176.  
  177. bool Play_MS(player a,player b,int l,int r,bool fir,bool ok){
  178. if(b.empty()) return ;
  179. player _a=a; int pls=r-l;
  180. if(a.CanBoom) if(Play_Boom(a,b,-,fir,)) return ;
  181. if(a.CanMS)
  182. for(int i=l+;i+pls<=;i++)
  183. if(a.CanPlayMoreSingle(i,i+pls)){
  184. a.PlayMoreSingle(i,i+pls);
  185. a.ResetCan();
  186. bool now=Play_MS(b,a,i,i+pls,,);
  187. if(!now){
  188. if(fir) Out_MS(i,i+pls);
  189. END;
  190. }
  191. a=_a;
  192. }
  193. NXT;
  194. }
  195.  
  196. bool Play_MC(player a,player b,int l,int r,bool fir,bool ok){
  197. if(b.empty()) return ;
  198. player _a=a; int pls=r-l;
  199. if(a.CanBoom) if(Play_Boom(a,b,-,fir,)) return ;
  200. if(a.CanMC)
  201. for(int i=l+;i+pls<=;i++)
  202. if(a.CanPlayMoreCouple(i,i+pls)){
  203. a.PlayMoreCouple(i,i+pls);
  204. a.ResetCan();
  205. bool now=Play_MC(b,a,i,i+pls,,);
  206. if(!now){
  207. if(fir) Out_MC(i,i+pls);
  208. END;
  209. }
  210. a=_a;
  211. }
  212. NXT;
  213. }
  214.  
  215. bool play(player a,player b,bool fir=){
  216. if(b.empty()) return ;
  217. if(a.CanBoom) if(Play_Boom(a,b,-,fir,)) return ;
  218. if(a.CanMS)
  219. for(int i=;i>=;i--){
  220. if(Play_MS(a,b,-,-+i,fir,)) return ;
  221. }
  222. if(a.CanMC)
  223. for(int i=;i>=;i--){
  224. if(Play_MC(a,b,-,-+i,fir,)) return ;
  225. }
  226. if(a.CanThree) if(Play_Three(a,b,-,fir,)) return ;
  227. if(a.CanThree) if(Play_3Single(a,b,-,fir,)) return ;
  228. if(a.CanThree) if(Play_3Couple(a,b,-,fir,)) return ;
  229. if(Play_Couple(a,b,-,fir,)) return ;
  230. if(Play_Single(a,b,-,fir,)) return ;
  231. if(a.CanBoom) if(Play_4Single(a,b,-,fir,)) return ;
  232. if(a.CanBoom) if(Play_4Couple(a,b,-,fir,)) return ;
  233. return ;
  234. }
  235.  
  236. int Main(){
  237. player a,b;
  238. printf("INPUT AI:\n");
  239. b.read();
  240. printf("INPUT PLAYER:\n");
  241. a.read();
  242. if(!play(a,b,)){
  243. printf("DIE\n");
  244. return ;
  245. }
  246. while(){
  247. a=newA; b=newB;
  248. printf("\n");
  249. if(a.empty()){
  250. printf("WIN\n");
  251. return ;
  252. }
  253.  
  254. string now; cin>>now;
  255. if(now=="EXIT") return ;
  256. if(Is_Empty(now)){
  257. if(!play(a,b,)){
  258. printf("DIE\n");
  259. }
  260. continue;
  261. }
  262. for(int i=;i<now.length();i++) b.PlaySingle(GetVal(now[i]));
  263.  
  264. if(Is_Single(now)&&Play_Single(a,b,GetVal(now[]),,)==) NO;
  265. if(Is_Couple(now)&&Play_Couple(a,b,GetVal(now[]),,)==) NO;
  266. if(Is_Three(now)&&Play_Three(a,b,GetVal(now[]),,)==) NO;
  267. if(Is_Boom(now)&&Play_Boom(a,b,GetVal(now[]),,)==) NO;
  268. if(Is_Rocket(now)&&Play_Rocket(a,b,)==) NO;
  269. if(Is_3Single(now)&&Play_3Single(a,b,GetVal(now[]),,)==) NO;
  270. if(Is_3Couple(now)&&Play_3Couple(a,b,GetVal(now[]),,)==) NO;
  271. if(Is_4Single(now)&&Play_4Single(a,b,GetVal(now[]),,)==) NO;
  272. if(Is_4Couple(now)&&Play_4Couple(a,b,GetVal(now[]),,)==) NO;
  273. if(Is_MS(now)&&Play_MS(a,b,GetVal(now[]),GetVal(now[now.length()-]),,)==) NO;
  274. if(Is_MC(now)&&Play_MC(a,b,GetVal(now[]),GetVal(now[now.length()-]),,)==) NO;
  275. }
  276. }
  277.  
  278. int main(){
  279. while() Main();
  280. }

main-v2.0

  1. /*
  2. ???????????????????
  3. ??
  4. ??????
  5. */
  6.  
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9.  
  10. int GetVal(char c){
  11. if(c=='T') return ;
  12. if(c=='J') return ;
  13. if(c=='Q') return ;
  14. if(c=='K') return ;
  15. if(c=='A') return ;
  16. if(c=='') return ;
  17. if(c=='w') return ;
  18. if(c=='W') return ;
  19. if(''<=c&&c<='') return c-'';
  20. assert();
  21. }
  22. char SetChar(int x){
  23. if(x<=) return ''+x;
  24. if(x==) return 'T';
  25. if(x==) return 'J';
  26. if(x==) return 'Q';
  27. if(x==) return 'K';
  28. if(x==) return 'A';
  29. if(x==) return '';
  30. if(x==) return 'w';
  31. if(x==) return 'W';
  32. assert();
  33. }
  34.  
  35. struct player{
  36. int a[];
  37. bool CanThree,CanBoom,CanMS,CanMC;
  38. player(){memset(a,,sizeof(a));}
  39.  
  40. void out(){//Output Poker
  41. for(int i=;i<;i++)
  42. for(int j=;j<a[i];j++)
  43. putchar(SetChar(i));
  44. putchar('\n');
  45. }
  46.  
  47. inline bool CanPlaySingle(int x){return a[x]>=;}//?
  48.  
  49. inline bool CanPlayCouple(int x){return a[x]>=;}//?
  50.  
  51. inline bool CanPlayThree(int x){return a[x]>=;}//??
  52.  
  53. inline bool CanPlayBoom(int x){return a[x]>=;}//??
  54.  
  55. inline bool CanPlayRocket(){return a[]&&a[];}//??
  56.  
  57. inline bool CanPlayMoreSingle(int l,int r){//??
  58. if(r-l+<||r>=) return ;
  59. for(int i=l;i<=r;i++) if(!CanPlaySingle(i)) return ;
  60. return ;
  61. }
  62.  
  63. inline bool CanPlayMoreCouple(int l,int r){//??
  64. if(r-l+<||r>=) return ;
  65. for(int i=l;i<=r;i++) if(!CanPlayCouple(i)) return ;
  66. return ;
  67. }
  68.  
  69. inline bool CanPlayMoreThree(int l,int r){//??
  70. if(r-l+<||r>=) return ;
  71. for(int i=l;i<=r;i++) if(!CanPlayThree(i)) return ;
  72. return ;
  73. }
  74.  
  75. inline bool CanPlay4Single(int x,int l,int r){//???
  76. if(x==l||x==r) return ;
  77. if(l==r) return CanPlayBoom(x)&&CanPlayCouple(l);
  78. return CanPlayBoom(x)&&CanPlaySingle(l)&&CanPlaySingle(r);
  79. }
  80.  
  81. inline bool CanPlay4Couple(int x,int l,int r){//???
  82. if(x==l||x==r) return ;
  83. if(l==r) return CanPlayBoom(x)&&CanPlayBoom(l);
  84. return CanPlayBoom(x)&&CanPlayCouple(l)&&CanPlayCouple(r);
  85. }
  86.  
  87. inline bool CanPlay3Single(int x,int y){
  88. if(x==y) return ;
  89. return CanPlayThree(x)&&CanPlaySingle(y);
  90. }
  91.  
  92. inline bool CanPlay3Couple(int x,int y){
  93. if(x==y) return ;
  94. return CanPlayThree(x)&&CanPlayCouple(y);
  95. }
  96.  
  97. inline void PlaySingle(int x){a[x]--;}
  98.  
  99. inline void PlayCouple(int x){a[x]-=;}
  100.  
  101. inline void PlayThree(int x){a[x]-=;}
  102.  
  103. inline void PlayBoom(int x){a[x]-=;}
  104.  
  105. inline void PlayRocket(){a[]--;a[]--;}
  106.  
  107. inline void PlayMoreSingle(int l,int r){for(int i=l;i<=r;i++) PlaySingle(i);}
  108.  
  109. inline void PlayMoreCouple(int l,int r){for(int i=l;i<=r;i++) PlayCouple(i);}
  110.  
  111. inline void PlayMoreThree(int l,int r){for(int i=l;i<=r;i++) PlayThree(i);}
  112.  
  113. inline void Play4Single(int x,int l,int r){PlayBoom(x); PlaySingle(l); PlaySingle(r);}
  114.  
  115. inline void Play4Couple(int x,int l,int r){PlayBoom(x); PlayCouple(l); PlayCouple(r);}
  116.  
  117. inline void Play3Single(int x,int y){PlayThree(x); PlaySingle(y);}
  118.  
  119. inline void Play3Couple(int x,int y){PlayThree(x); PlayCouple(y);}
  120.  
  121. inline bool empty(){
  122. for(int i=;i<;i++)
  123. if(a[i])
  124. return ;
  125. return ;}//??????
  126.  
  127. inline long long Hash(){long long res=; for(int i=;i<;i++) res=res<<|a[i]; return res;}//???
  128.  
  129. inline int GetMin(){for(int i=;i<;i++) if(a[i]) return i; assert();}
  130. inline int GetMax(){for(int i=;~i;i--) if(a[i]) return i; return -;}
  131.  
  132. inline void ResetCan(){
  133. CanThree=CanBoom=CanMS=CanMC=;//!!!
  134. for(int i=;i<;i++){
  135. if(CanPlayThree(i)) CanThree=;
  136. if(CanPlayBoom(i)) CanBoom=;
  137. }
  138. if(CanPlayRocket()) CanBoom=;
  139. for(int i=;i<=;i++){
  140. if(CanPlayMoreSingle(i,i+)) {CanMS=; break;}
  141. }
  142. for(int i=;i<=;i++){
  143. if(CanPlayMoreCouple(i,i+)) {CanMC=; break;}
  144. }
  145. }
  146.  
  147. void read(){//Input Poker
  148. memset(a,,sizeof(a));
  149. string s; cin>>s; int len=s.length();
  150. for(int i=;i<len;i++)
  151. a[GetVal(s[i])]++;
  152. ResetCan();
  153. }
  154. };

Player-v2.0

众所周知欢乐斗地主这个游戏有一个叫做残局的模式

模式的特征:你和机器人都互相知道对方的牌,且对面的机器人绝顶聪明。

正常的斗地主规则,问你要如何出牌,才能保证你一定能赢。

我们考虑直接爆搜,发现需要搜一年。

于是我们加一个Alpha-Beta剪枝来加速整个爆搜过程即可。

貌似绝大部分情况下都是秒出结果的

目前最长时间只搜了15s(这是v2.0的时间,旧版貌似接近一分钟了)

正确率100%,快速通关利器233333

输入牌型要求:2~9直接输入,JQKA直接输入,10用T表示,大王小王分别用Ww表示。

输入顺子/连对要从小到大输入(按照斗地主规则的从小到大),输入三带一/对,四带二/对,需要先输入三张/四张,再输入剩下的东西

代码貌似只要10k多一点

注意细节

两个关联文件的代码:

  1. /*
  2. 该部分函数用于输出打出的牌
  3. 以及
  4. 分析当前输入的牌型
  5.  
  6. */
  7.  
  8. inline void Out_Single(int x){printf("%c",SetChar(x));}
  9.  
  10. inline void Out_Couple(int x){printf("%c%c",SetChar(x),SetChar(x));}
  11.  
  12. inline void Out_Three(int x){printf("%c%c%c",SetChar(x),SetChar(x),SetChar(x));}
  13.  
  14. inline void Out_Boom(int x){printf("%c%c%c%c",SetChar(x),SetChar(x),SetChar(x),SetChar(x));}
  15.  
  16. inline void Out_3Single(int x,int y){Out_Three(x); Out_Single(y);}
  17.  
  18. inline void Out_3Couple(int x,int y){Out_Three(x); Out_Couple(y);}
  19.  
  20. inline void Out_MS(int l,int r){for(int i=l;i<=r;i++) Out_Single(i);}
  21.  
  22. inline void Out_MC(int l,int r){for(int i=l;i<=r;i++) Out_Couple(i);}
  23.  
  24. inline void Out_M3(int l,int r){for(int i=l;i<=r;i++) Out_Three(i);}
  25.  
  26. inline void Out_Rocket(){printf("Ww");}
  27.  
  28. inline void Out_4Single(int x,int l,int r){Out_Boom(x); Out_Single(l); Out_Single(r);}
  29.  
  30. inline void Out_4Couple(int x,int l,int r){Out_Boom(x); Out_Couple(l); Out_Couple(r);}
  31.  
  32. inline bool Is_Empty(string s){return s.length()==&&s[]=='*';}
  33.  
  34. inline bool Is_Single(string s){return s.length()==;}
  35.  
  36. inline bool Is_Rocket(string s){return s=="Ww"||s=="wW";}
  37.  
  38. inline bool Is_Couple(string s){return s.length()==&&s[]==s[];}
  39.  
  40. inline bool Is_Three(string s){return s.length()==&&s[]==s[]&&s[]==s[];}
  41.  
  42. inline bool Is_Boom(string s){return s.length()==&&s[]==s[]&&s[]==s[]&&s[]==s[];}
  43.  
  44. inline bool Is_3Single(string s){return s.length()==&&s[]==s[]&&s[]==s[]&&s[]!=s[];}
  45.  
  46. inline bool Is_3Couple(string s){return s.length()==&&s[]==s[]&&s[]==s[]&&s[]!=s[]&&s[]==s[];}
  47.  
  48. inline bool Is_MS(string s){
  49. int len=s.length(); if(len<) return ;
  50. for(int i=;i<len;i++) if(GetVal(s[i-])+!=GetVal(s[i])) return ;
  51. return ;
  52. }
  53.  
  54. inline bool Is_MC(string s){
  55. int len=s.length(); if((len&)||len<) return ;
  56. for(int i=;i<len;i+=){
  57. if(s[i]!=s[i-]) return ;
  58. }
  59. for(int i=;i<len;i+=){
  60. if(GetVal(s[i-])+!=GetVal(s[i])) return ;
  61. }
  62. return ;
  63. }
  64.  
  65. inline bool Is_4Single(string s){return s.length()==&&s[]==s[]&&s[]==s[]&&s[]==s[]&&s[]!=s[];}
  66.  
  67. inline bool Is_4Couple(string s){return s.length()==&&s[]==s[]&&s[]==s[]&&s[]==s[]&&s[]!=s[]&&s[]==s[]&&s[]==s[];}

Helper.cpp

  1. /*
  2. 该部分函数用于判定是否可以出该类型的牌
  3. 以及
  4. 模拟出牌过程
  5. */
  6.  
  7. #include<bits/stdc++.h>
  8. using namespace std;
  9.  
  10. int GetVal(char c){
  11. if(c=='T') return ;
  12. if(c=='J') return ;
  13. if(c=='Q') return ;
  14. if(c=='K') return ;
  15. if(c=='A') return ;
  16. if(c=='') return ;
  17. if(c=='w') return ;
  18. if(c=='W') return ;
  19. if(''<=c&&c<='') return c-'';
  20. assert();
  21. }
  22. char SetChar(int x){
  23. if(x<=) return ''+x;
  24. if(x==) return 'T';
  25. if(x==) return 'J';
  26. if(x==) return 'Q';
  27. if(x==) return 'K';
  28. if(x==) return 'A';
  29. if(x==) return '';
  30. if(x==) return 'w';
  31. if(x==) return 'W';
  32. assert();
  33. }
  34.  
  35. struct player{
  36. int a[];
  37. player(){memset(a,,sizeof(a));}
  38. void read(){//Input Poker
  39. memset(a,,sizeof(a));
  40. string s; cin>>s; int len=s.length();
  41. for(int i=;i<len;i++)
  42. a[GetVal(s[i])]++;
  43. }
  44.  
  45. void out(){//Output Poker
  46. for(int i=;i<;i++)
  47. for(int j=;j<a[i];j++)
  48. putchar(SetChar(i));
  49. putchar('\n');
  50. }
  51.  
  52. inline bool CanPlaySingle(int x){return a[x]>=;}//单
  53.  
  54. inline bool CanPlayCouple(int x){return a[x]>=;}//对
  55.  
  56. inline bool CanPlayThree(int x){return a[x]>=;}//三张
  57.  
  58. inline bool CanPlayBoom(int x){return a[x]>=;}//炸弹
  59.  
  60. inline bool CanPlayRocket(){return a[]&&a[];}//火箭
  61.  
  62. inline bool CanPlayMoreSingle(int l,int r){//顺子
  63. if(r-l+<||r>=) return ;
  64. for(int i=l;i<=r;i++) if(!CanPlaySingle(i)) return ;
  65. return ;
  66. }
  67.  
  68. inline bool CanPlayMoreCouple(int l,int r){//连对
  69. if(r-l+<||r>=) return ;
  70. for(int i=l;i<=r;i++) if(!CanPlayCouple(i)) return ;
  71. return ;
  72. }
  73.  
  74. inline bool CanPlayMoreThree(int l,int r){//飞机
  75. if(r-l+<||r>=) return ;
  76. for(int i=l;i<=r;i++) if(!CanPlayThree(i)) return ;
  77. return ;
  78. }
  79.  
  80. inline bool CanPlay4Single(int x,int l,int r){//四带二
  81. if(x==l||x==r) return ;
  82. if(l==r) return CanPlayBoom(x)&&CanPlayCouple(l);
  83. return CanPlayBoom(x)&&CanPlaySingle(l)&&CanPlaySingle(r);
  84. }
  85.  
  86. inline bool CanPlay4Couple(int x,int l,int r){//四带对
  87. if(x==l||x==r) return ;
  88. if(l==r) return CanPlayBoom(x)&&CanPlayBoom(l);
  89. return CanPlayBoom(x)&&CanPlayCouple(l)&&CanPlayCouple(r);
  90. }
  91.  
  92. inline bool CanPlay3Single(int x,int y){
  93. if(x==y) return ;
  94. return CanPlayThree(x)&&CanPlaySingle(y);
  95. }
  96.  
  97. inline bool CanPlay3Couple(int x,int y){
  98. if(x==y) return ;
  99. return CanPlayThree(x)&&CanPlayCouple(y);
  100. }
  101.  
  102. inline void PlaySingle(int x){a[x]--;}
  103.  
  104. inline void PlayCouple(int x){a[x]-=;}
  105.  
  106. inline void PlayThree(int x){a[x]-=;}
  107.  
  108. inline void PlayBoom(int x){a[x]-=;}
  109.  
  110. inline void PlayRocket(){a[]--;a[]--;}
  111.  
  112. inline void PlayMoreSingle(int l,int r){for(int i=l;i<=r;i++) PlaySingle(i);}
  113.  
  114. inline void PlayMoreCouple(int l,int r){for(int i=l;i<=r;i++) PlayCouple(i);}
  115.  
  116. inline void PlayMoreThree(int l,int r){for(int i=l;i<=r;i++) PlayThree(i);}
  117.  
  118. inline void Play4Single(int x,int l,int r){PlayBoom(x); PlaySingle(l); PlaySingle(r);}
  119.  
  120. inline void Play4Couple(int x,int l,int r){PlayBoom(x); PlayCouple(l); PlayCouple(r);}
  121.  
  122. inline void Play3Single(int x,int y){PlayThree(x); PlaySingle(y);}
  123.  
  124. inline void Play3Couple(int x,int y){PlayThree(x); PlayCouple(y);}
  125.  
  126. inline bool empty(){
  127. for(int i=;i<;i++)
  128. if(a[i])
  129. return ;
  130. return ;}//判断是否出完
  131.  
  132. inline long long Hash(){long long res=; for(int i=;i<;i++) res=res<<|a[i]; return res;}//哈希值
  133.  
  134. inline int GetMin(){for(int i=;i<;i++) if(a[i]) return i; assert();}
  135. inline int GetMax(){for(int i=;~i;i--) if(a[i]) return i; return -;}
  136. };

Player.cpp

主代码:

  1. #include "Player.cpp"
  2. #include "Helper.cpp"
  3.  
  4. #define END {if(fir){newA=a; newB=b;} return 1;}
  5. #define NXT {if(ok&&play(b,a,0)==0) return 1; else return 0;}
  6. #define NO {printf("Can't Out\n"); newA=a; newB=b;}
  7. player newA,newB;
  8. bool play(player,player,bool);
  9.  
  10. bool Play_Rocket(player a,player b,bool fir){
  11. if(b.empty()) return ;
  12. if(a.CanPlayRocket()){
  13. a.PlayRocket();
  14. bool now=play(a,b,);
  15. if(now){
  16. if(fir) Out_Rocket();
  17. END;
  18. }
  19. }
  20. return ;
  21. }
  22.  
  23. bool Play_Boom(player a,player b,int last,bool fir,bool ok){
  24. if(b.empty()) return ;
  25. if(Play_Rocket(a,b,fir)) return ;
  26. player _a=a;
  27. for(int i=last+;i<;i++)
  28. if(a.CanPlayBoom(i)){
  29. a.PlayBoom(i);
  30. bool now=Play_Boom(b,a,i,,);
  31. if(!now){
  32. if(fir) Out_Boom(i);
  33. END;
  34. }
  35. a=_a;
  36. }
  37. NXT;
  38. }
  39.  
  40. bool Play_Single(player a,player b,int last,bool fir,bool ok){
  41. if(b.empty()) return ;
  42. player _a=a;
  43. if(Play_Boom(a,b,-,fir,)) return ;
  44. for(int i=last+;i<;i++)
  45. if(a.CanPlaySingle(i)){
  46. a.PlaySingle(i);
  47. bool now=Play_Single(b,a,i,,);
  48. if(!now){
  49. if(fir) Out_Single(i);
  50. END;
  51. }
  52. a=_a;
  53. }
  54. NXT;
  55. }
  56.  
  57. bool Play_Couple(player a,player b,int last,bool fir,bool ok){
  58. if(b.empty()) return ;
  59. player _a=a;
  60. if(Play_Boom(a,b,-,fir,)) return ;
  61. for(int i=last+;i<;i++)
  62. if(a.CanPlayCouple(i)){
  63. a.PlayCouple(i);
  64. bool now=Play_Couple(b,a,i,,);
  65. if(!now){
  66. if(fir) Out_Couple(i);
  67. END;
  68. }
  69. a=_a;
  70. }
  71. NXT;
  72. }
  73.  
  74. bool Play_Three(player a,player b,int last,bool fir,bool ok){
  75. if(b.empty()) return ;
  76. player _a=a;
  77. if(Play_Boom(a,b,-,fir,)) return ;
  78. for(int i=last+;i<;i++)
  79. if(a.CanPlayThree(i)){
  80. a.PlayThree(i);
  81. bool now=Play_Three(b,a,i,,);
  82. if(!now){
  83. if(fir) Out_Three(i);
  84. END;
  85. }
  86. a=_a;
  87. }
  88. NXT;
  89. }
  90.  
  91. bool Play_3Single(player a,player b,int last,bool fir,bool ok){
  92. if(b.empty()) return ;
  93. player _a=a;
  94. if(Play_Boom(a,b,-,fir,)) return ;
  95. for(int i=last+;i<;i++) if(a.CanPlayThree(i))
  96. for(int j=;j<;j++)
  97. if(a.CanPlay3Single(i,j)){
  98. a.Play3Single(i,j);
  99. bool now=Play_3Single(b,a,i,,);
  100. if(!now){
  101. if(fir) Out_3Single(i,j);
  102. END;
  103. }
  104. a=_a;
  105. }
  106. NXT;
  107. }
  108.  
  109. bool Play_3Couple(player a,player b,int last,bool fir,bool ok){
  110. if(b.empty()) return ;
  111. player _a=a;
  112. if(Play_Boom(a,b,-,fir,)) return ;
  113. for(int i=last+;i<;i++) if(a.CanPlayThree(i))
  114. for(int j=;j<;j++)
  115. if(a.CanPlay3Couple(i,j)){
  116. a.Play3Couple(i,j);
  117. bool now=Play_3Couple(b,a,i,,);
  118. if(!now){
  119. if(fir) Out_3Couple(i,j);
  120. END;
  121. }
  122. a=_a;
  123. }
  124. NXT;
  125. }
  126.  
  127. bool Play_4Single(player a,player b,int last,bool fir,bool ok){
  128. if(b.empty()) return ;
  129. player _a=a;
  130. if(Play_Boom(a,b,-,fir,)) return ;
  131. for(int i=last+;i<;i++) if(a.CanPlayBoom(i))
  132. for(int j=;j<;j++)
  133. for(int k=;k<;k++)
  134. if(a.CanPlay4Single(i,j,k)){
  135. a.Play4Single(i,j,k);
  136. bool now=Play_4Single(b,a,i,,);
  137. if(!now){
  138. if(fir) Out_4Single(i,j,k);
  139. END;
  140. }
  141. a=_a;
  142. }
  143. NXT;
  144. }
  145.  
  146. bool Play_4Couple(player a,player b,int last,bool fir,bool ok){
  147. if(b.empty()) return ;
  148. player _a=a;
  149. if(Play_Boom(a,b,-,fir,)) return ;
  150. for(int i=last+;i<;i++) if(a.CanPlayBoom(i))
  151. for(int j=;j<;j++)
  152. for(int k=;k<;k++)
  153. if(a.CanPlay4Couple(i,j,k)){
  154. a.Play4Couple(i,j,k);
  155. bool now=Play_4Couple(b,a,i,,);
  156. if(!now){
  157. if(fir) Out_4Couple(i,j,k);
  158. END;
  159. }
  160. a=_a;
  161. }
  162. NXT;
  163. }
  164.  
  165. bool Play_MS(player a,player b,int l,int r,bool fir,bool ok){
  166. if(b.empty()) return ;
  167. player _a=a; int pls=r-l;
  168. if(Play_Boom(a,b,-,fir,)) return ;
  169. for(int i=l+;i+pls<=;i++)
  170. if(a.CanPlayMoreSingle(i,i+pls)){
  171. a.PlayMoreSingle(i,i+pls);
  172. bool now=Play_MS(b,a,i,i+pls,,);
  173. if(!now){
  174. if(fir) Out_MS(i,i+pls);
  175. END;
  176. }
  177. a=_a;
  178. }
  179. NXT;
  180. }
  181.  
  182. bool Play_MC(player a,player b,int l,int r,bool fir,bool ok){
  183. if(b.empty()) return ;
  184. player _a=a; int pls=r-l;
  185. if(Play_Boom(a,b,-,fir,)) return ;
  186. for(int i=l+;i+pls<=;i++)
  187. if(a.CanPlayMoreCouple(i,i+pls)){
  188. a.PlayMoreCouple(i,i+pls);
  189. bool now=Play_MC(b,a,i,i+pls,,);
  190. if(!now){
  191. if(fir) Out_MC(i,i+pls);
  192. END;
  193. }
  194. a=_a;
  195. }
  196. NXT;
  197. }
  198.  
  199. bool play(player a,player b,bool fir=){
  200. if(b.empty()) return ;
  201. if(Play_Boom(a,b,-,fir,)) return ;
  202. for(int i=;i>=;i--){
  203. if(Play_MS(a,b,-,-+i,fir,)) return ;
  204. }
  205. for(int i=;i>=;i--){
  206. if(Play_MC(a,b,-,-+i,fir,)) return ;
  207. }
  208. if(Play_Three(a,b,-,fir,)) return ;
  209. if(Play_3Single(a,b,-,fir,)) return ;
  210. if(Play_3Couple(a,b,-,fir,)) return ;
  211. if(Play_Couple(a,b,-,fir,)) return ;
  212. if(Play_Single(a,b,-,fir,)) return ;
  213. if(Play_4Single(a,b,-,fir,)) return ;
  214. if(Play_4Couple(a,b,-,fir,)) return ;
  215. return ;
  216. }
  217.  
  218. int Main(){
  219. player a,b;
  220. printf("INPUT AI:\n");
  221. b.read();
  222. printf("INPUT PLAYER:\n");
  223. a.read();
  224. if(!play(a,b,)){
  225. printf("DIE\n");
  226. return ;
  227. }
  228. while(){
  229. a=newA; b=newB;
  230. printf("\n");
  231. if(a.empty()){
  232. printf("WIN\n");
  233. return ;
  234. }
  235.  
  236. string now; cin>>now;
  237. if(now=="EXIT") return ;
  238. if(Is_Empty(now)){
  239. if(!play(a,b,)){
  240. printf("DIE\n");
  241. }
  242. continue;
  243. }
  244. for(int i=;i<now.length();i++) b.PlaySingle(GetVal(now[i]));
  245.  
  246. if(Is_Single(now)&&Play_Single(a,b,GetVal(now[]),,)==) NO;
  247. if(Is_Couple(now)&&Play_Couple(a,b,GetVal(now[]),,)==) NO;
  248. if(Is_Three(now)&&Play_Three(a,b,GetVal(now[]),,)==) NO;
  249. if(Is_Boom(now)&&Play_Boom(a,b,GetVal(now[]),,)==) NO;
  250. if(Is_Rocket(now)&&Play_Rocket(a,b,)==) NO;
  251. if(Is_3Single(now)&&Play_3Single(a,b,GetVal(now[]),,)==) NO;
  252. if(Is_3Couple(now)&&Play_3Couple(a,b,GetVal(now[]),,)==) NO;
  253. if(Is_4Single(now)&&Play_4Single(a,b,GetVal(now[]),,)==) NO;
  254. if(Is_4Couple(now)&&Play_4Couple(a,b,GetVal(now[]),,)==) NO;
  255. if(Is_MS(now)&&Play_MS(a,b,GetVal(now[]),GetVal(now[now.length()-]),,)==) NO;
  256. if(Is_MC(now)&&Play_MC(a,b,GetVal(now[]),GetVal(now[now.length()-]),,)==) NO;
  257. }
  258. }
  259.  
  260. int main(){
  261. while() Main();
  262. }

基于Alpha-Beta剪枝的欢乐斗地主残局辅助的更多相关文章

  1. 扩增子分析解读6进化树 Alpha Beta多样性

    分析前准备 # 进入工作目录 cd example_PE250 上一节回顾:我们的OTU获得了物种注释,并学习OTU表的各种操作————添加信息,格式转换,筛选信息.   接下来我们学习对OTU序列的 ...

  2. (转)unity中基于alpha通道的shadow volume实现

    转自:http://blog.163.com/wmk_2000_ren/blog/static/138846192201019114117466/ 实现呢,Aras大神已经给出了, http://fo ...

  3. 《QQ欢乐斗地主》山寨版

    使用Cocos2d-x编写,模仿<QQ欢乐斗地主>的界面实现了一个具有简单AI的单机版斗地主游戏. 游戏的详细说明请查看游戏目录下的help.txt文件. 下载地址: http://dow ...

  4. 软件发布版本区别介绍-Alpha,Beta,RC,Release

    Alpha: Alpha是内部测试版,一般不向外部发布,会有很多Bug.除非你也是测试人员,否则不建议使用. 是希腊字母的第一位,表示最初级的版本 alpha就是α,beta就是β alpha版就是比 ...

  5. 软工+C(2017第4期) Alpha/Beta换人

    // 上一篇:超链接 // 下一篇:工具和结构化 注:在一次软件工程讨论课程进度设计的过程中,出现了这个关于 Alpha/Beta换人机制的讨论,这个机制在不同学校有不同的实施,本篇积累各方观点,持续 ...

  6. 软工+C(4): Alpha/Beta换人

    // 上一篇:超链接 // 下一篇:工具和结构化 注:在一次软件工程讨论课程进度设计的过程中,出现了这个关于 Alpha/Beta换人机制的讨论,这个机制在不同学校有不同的实施,本篇积累各方观点,持续 ...

  7. K 班1-7,alpha,beta 作业成绩汇总

    K 班1-7,alpha,beta 作业成绩汇总 千帆竞发 详细得分 短学号 名 1 2 3 4 5 6 7 alpha beta TOTAL 505 基智 4.55 1 -2 0 0 -10 4.3 ...

  8. 九度oj 题目1363:欢乐斗地主

    题目描述: 如果大家玩过欢乐斗地主这个游戏,就一定知道有一个具有“提示”功能的按钮.如果你不知道你现在手里的牌有没有比上家大的牌,并且你也懒得去一张一张地看你手中的牌.这时候你就可以点“提示”按钮,系 ...

  9. 软件版本GA,RC,alpha,beta含义

    软件版本GA,RC,alpha,beta含义 (1)RC:(Release Candidate) Candidate是候选人的意思,用在软件上就是候选版本.Release.Candidate.就是发行 ...

随机推荐

  1. Finance财务软件(自定义报表专题)

    我们可以通过存储过程自定义报表 1.在菜单中新增报表菜单,这里的代码约束为报表对应存储过程名称,配置完成成后重启客户端生效 2.在自定义模板中适配存储过程入参,这里的功能键值为存储过程名称,字段键值与 ...

  2. MySQL设置global变量和session变量

    1.在MySQL中要修改全局(global)变量,有两种方法: 方法一,修改my.ini配置文件,如果要设置全局变量最简单的方式是在my.ini文件中直接写入变量配置,如下图所示.重启数据库服务就可以 ...

  3. freeMark模板引擎

    http://blog.csdn.net/shimiso/article/details/8778793

  4. linux下maven的安装

    上传安装包到相应路径 进行解压 tar -zxvf apache-maven-3.0.5-bin.tar.gz -C /export/servers/ 修改环境变量 vim /etc/profile ...

  5. 每日一练ACM 2019.04.14

    2019.4.14 第1001题:Sum Problem Problem DescriptionHey, welcome to HDOJ(Hangzhou Dianzi University Onli ...

  6. python微信自动回复

    模块是itchat 下载:命令行输入 pip install itchat import itchat #导入itchat模块 itchat.auto_login() #登陆微信,授权 用命令行发送给 ...

  7. loadrunner整体压测执行操作步骤

    lr11安装包链接:https://pan.baidu.com/s/1hF3j2Vi_xB8BhT70P1ZdBg 提取码:n3zn lr12安装包链接:https://pan.baidu.com/s ...

  8. 在idea中,mavne项目使用mybatis-generator-maven-plugin自动生成实体了的时候,在maven插件里面始终不显示

    最近想学习mybatis的知识,自己搭了个简单的ssm框架,想通过插件自动生成实体类,发现想要的插件一直都没显示出来,着实很郁闷: pom.xm中的配置: <!--mybatis-generat ...

  9. 风格豆腐干地方v出vccxzzxx

    ksdfjlksdjflksdjlfjsdkflj{b7a6e0i010b7b7g2i010b7b7g2i010b7b7c8i010f1j4i010e0h3i010e0h3i010b7a6c8i010 ...

  10. java 判断用户是PC端和还是APP端登陆

    java 判断用户是PC端和还是APP端登陆 public void getRequestHeader(HttpServletRequest request){ // 从浏览器获取请求头信息 Stri ...