1#include <iostream>2 using namespacestd;3 4 introw,col,chess;5 Charboard[5][5];6 7 intMinsearch (intIintJintAlpha);8 intMaxsearch (intIintJintbeta);9 Ten One BOOLCheckintRintc) { A if(board[r][0]==board[r][1] && board[r][1]==board[r][2] && board[r][2]==board[r][3] && board[r][3]!='.')return true; - if(board[0][c]==board[1][C] && board[1][c]==board[2][C] && board[2][c]==board[3][C] && board[3][c]!='.')return true; - the if(board[0][3]==board[1][2] && board[1][2]==board[2][1] && board[2][1]==board[3][0] && board[0][3]==BOARD[R][C])return true; - if(board[0][0]==board[1][1] && board[1][1]==board[2][2] && board[2][2]==board[3][3] && board[0][0]==BOARD[R][C])return true; - return false; - } + - intMinsearch (intIintJintAlpha) {//Minsearch returns the "minimum" alpha in all child nodes representing the maximum value of its father's sibling node . + //The previous step in the (I,J), to determine whether "this piece" can let the game end A if(check (I,J))return 1;//x is over, X wins. at if(chess== -)return 0; - - - - - intBeta =1;//This is the minimum value of the sub-node = = because it is min node = = At the very beginning . in for(intI=0;i<4; i++) - for(intj=0;j<4; j + +) to if(board[i][j]=='.' ){ +BOARD[I][J] ='o'; chess++; - thebeta = min (Beta, maxsearch (I,j,beta));//The third parameter is = = = The minimum value of the current min node sibling node. * $BOARD[I][J] ='.'; chess--;Panax Notoginseng if(alpha>= Beta)returnBeta; - } the returnBeta; + } A the intMaxsearch (intIintJintBeta) {//Beta is the smallest value in the upper sibling node + if(check (I,J))return-1; - if(chess== -)return 0; $ $ intAlpha =-1;//record the maximum value in all child nodes - for(intI=0;i<4; i++) - for(intj=0;j<4; j + +) the if(board[i][j]=='.' ){ -BOARD[I][J] ='x'; chess++;Wuyi theAlpha = max (Alpha,minsearch (I,j,alpha));//Alpha is the maximum value of my brother's knot. - WuBOARD[I][J] ='.'; chess--; - if(Alpha>=beta)returnAlpha; About } $ returnAlpha; - } - - A + BOOLsolve () { the - intalpha=-1;//at first, the maximum value is very small = = root node = = root node of the child nodes are not cut . $ for(intI=0;i<4; i++) the for(intj=0;j<4; j + +) the if(board[i][j]=='.' ){ theBOARD[I][J] ='x'; chess++; the - intTMP = Minsearch (I,j,alpha);//The third is that alpha represents the maximum value of its sibling node, but Root has no sibling nodes in theBOARD[I][J] ='.'; chess--; the //cout<<i<< "" <<j<< "" <<tmp<<endl; About //cout<<i<< "" <<j<<endl; the if(tmp==1) {row=i; col=j;return true; } the } the + return false; - } the Bayi the intMain () { the - - while(1){ thechess=0; the CharOp Cin>>op; the if(op=='$') Break; the for(intI=0;i<4; i++) - for(intj=0;j<4; j + +) { theCin>>Board[i][j]; the if(board[i][j]!='.') chess++; the }94 the if(Solve ()) cout<<"("<<row<<","<<col<<")"<<Endl; the Elsecout<<"#####"<<Endl; the }98 About -}
POJ Find the Winning Move "Minmax Search +alpha-beta Pruning" "PKU ACM/ICPC Competition Training"