Poj1568 find the winning move -- find the winning move extremely small search

Source: Internet
Author: User
Find the winning move
Time limit:3000 Ms   Memory limit:32768 K
Total submissions:421   Accepted:240

Description

4x4 tic-tac-toe is played on a board with four rows (numbered 0 to 3 from top to bottom) and four columns (numbered 0 to 3 from left to right ). there are two players, X and O, who move alternately with X always going first. the
Game is won by the first player to get four of his or her pieces on the same row, column, or diagonal. if the Board is full and neither player has won then the game is a draw.
Assuming that it is X's turn to move, X is said to have a forced win if X can make a move such that no matter what moves O makes for the rest of the game, X can win. this does not necessarily mean that X will win on the very next move, although that is a possibility.
It means that X has a winning strategy that will guarantee an eventual vicw.regardless of what o does.

Your job is to write a program that, given a partially-completed game with X to move next, will determine whether X has a forced win. you can assume that each player has made at least two moves, that the game has not already been won by either player, and that
The Board is not full.

Input

The input contains one or more test cases, followed by a line beginning with a dollar sign that signals the end of the file. each test case begins with a line containing a question mark and is followed by four lines representing
The Board; formatting is exactly as shown in the example. the characters used in a board description are the period (representing an empty space), lowercase X, and lowercase O. for each test case, output a line containing the (row, column) Position of
First forced win for X, or '####' if there is no forced win. format the output exactly as shown in the example.

Output

For this problem, the first forced win is determined by board position, not the number of moves required for vicred. search for a forced win by examining positions (0, 0), (0, 1), (0, 2), (0, 3), (1, 0 ), (1, 1 ),..., (3, 2), (3,
3), in that order, and output the first forced win you find. in the second test case below, note that X cocould win immediately by playing at (0, 3) Or (2, 0), but playing at (0, 1) will still ensure vicugh (although it unnecessarily delays it), and position
(0, 1) comes first.

Sample Input

?.....xo..ox.....?o....ox..xxxxooo$

Sample output

#####(0,1)

Source

Mid-Central USA 1999 code reference this example.

Extremely small search strategies are generally used in NLP games:

In this way, the deep search policy is used in essence, so it can be implemented using recursive methods. In the search process, the maximum value should be obtained for the search point advantageous to the local party, and the minimum value should be obtained for the search point unfavorable to the local party.

The minimum and maximum values are relative.

In the search process, you need to properly control the search depth. The deeper the search depth, the lower the efficiency. But in general, the better the search method.

Extremely small searches can be written separately or written together.

# Include <iostream> # include <memory. h> # include <stdio. h> using namespace STD; # define INF contains invalid int State [5] [5], chess, XI, XJ; char ch; int minfind (INT, Int, INT ); int maxfind (INT, Int, INT); bool over (int x, int y) {bool flag = false; int row [5], Col [5]; memset (row, 0, sizeof (ROW); memset (COL, 0, sizeof (COL); For (INT I = 0; I <4; I ++) for (Int J = 0; j <4; j ++) {If (State [I] [J] = 'X') {row [I] ++; col [J] ++;} If (State [I] [J] = 'O') {row [I] --; Col [J] --;} if (row [x] =-4 | row [x] = 4 | Col [y] =-4 | Col [y] = 4) flag = true; int tot1 = 0, tot2 = 0; For (INT I = 0; I <4; I ++) {If (State [I] [I] = 'X') tot1 ++; If (State [I] [3-I] = 'X') tot2 ++; if (State [I] [I] = 'O') tot1 --; If (State [I] [3-I] = 'O') tot2 --;} if (tot1 = 4 | tot1 =-4) & X = y) Flag = true; if (tot2 = 4 | tot2 =-4) & X = 3-y) Flag = true; return flag;} int Max Find (int x, int y, int mini) {int TMP, Maxi =-INF; If (over (x, y) return Maxi; If (ChEss = 16) return 0; For (INT I = 0; I <4; I ++) for (Int J = 0; j <4; j ++) if (State [I] [J] = '. ') {State [I] [J] = 'X'; chess ++; TMP = minfind (I, j, Maxi); chess --; state [I] [J] = '. '; Maxi = max (Maxi, TMP); If (Maxi> = mini) return Maxi; // F (p) is infinite, first hand wins} return Maxi ;} int minfind (int x, int y, int Maxi) {int TMP, mini = inf; If (over (x, y) r Eturn mini; If (ChEss = 16) return 0; For (INT I = 0; I <4; I ++) for (Int J = 0; j <4; j ++) if (State [I] [J] = '. ') {State [I] [J] = 'O'; chess ++; TMP = maxfind (I, j, mini); chess --; state [I] [J] = '. '; Mini = min (Mini, TMP); If (mini <= Maxi) return mini; // F (p) negative infinity, wins in the future} return mini ;} bool tryit () {int TMP, Maxi =-INF; For (INT I = 0; I <4; I ++) for (Int J = 0; j <4; j ++) if (State [I] [J] = '. ') {State [I] [J] = 'X'; chess ++; TMP = Minfind (I, j, Maxi); chess --; State [I] [J] = '. '; If (TMP> = Maxi) {Maxi = TMP; xi = I; XJ = J;} If (Maxi = inf) return true; // F (p) positive infinity, first hand wins} return false;} int main () {While (scanf ("% C", & Ch) {If (CH = '$') break; scanf ("% C", & Ch); chess =-4; for (INT I = 0; I <4; I ++) for (Int J = 0; j <5; j ++) {scanf ("% C", & State [I] [J]); chess + = State [I] [J]! = '. ';} If (ChEss <= 4) {// strong pruning printf ("#####\ N"); continue;} If (tryit ()) printf ("(% d, % d) \ n", XI, XJ); else printf ("#####\ N") ;}return 0 ;}
Alpha-beta search pruning alpha-beta search pruning generally, in order to cause pruning during the search process, two parameters must be passed down during recursion. The first parameter is Alpha, which indicates the best value found by one of the current search nodes. Any value smaller than it makes no sense. The 2nd values are beta, indicating the current disadvantage of the opponent. This is the best result that the opponent can bear, and the values greater than it will be discarded.

The alpha-beta search pruning algorithm is as follows:

Input: search depth, alpha, beta

Output: the optimal route of the node and the corresponding optimal Valuation

Function Format: int alphabetasearch (INT depth, int Alpha, int beta)

If depth is less than or equal to 0

Call the evaluation function and assign the result to the value

Return Value

Otherwise

Generate all reasonable current steps

For every walk

Execution steps

Value =-alphabetasearch (depth-1,-Beta,-alpha );

// Note that there is a negative value before the function, and the alpha and beta parameters take negative values and exchange them.

Withdrawal steps

If value> = beta // you can search for a method first, because the search efficiency depends largely on the pruning effect.

Return to Beta

If value> alpha

Alpha = Value

If depth = max_depth

Bestmove = mv

Returns Alpha.

Call method: alphabetasearch (maxdepth,-maxvalue, maxvalue)

The code written is not exactly the same as this idea.

#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <stack>#include <vector>#include <string>#include <map>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;const int inf=(1<<30);const int N=25;int pw2[25],pw3[25];map<int,int>rpw2;//---------------------------------------------------------struct point {    int x,y;}pt[N];int m;char g[5][5];int ansx,ansy,score;int ans[N],ct;int hash[555555];const int n=4;bool check(int x,int y){    int i=0;    while(i<4&&g[x][i]==g[x][y]) i++;if(i>=4) return true;i=0;    while(i<4&&g[i][y]==g[x][y]) i++;if(i>=4) return true;i=0;    if(x==y)    while(i<4&&g[i][i]==g[x][y]) i++;if(i>=4) return true;i=0;    if(x+y==3)    while(i<4&&g[i][3-i]==g[x][y]) i++;if(i>=4) return true;    return false;}int maxmin(int state,int who,int now,int alpha,int beta,int dep) {    if(hash[now]!=-inf) return hash[now];    if(state==0) return hash[now]=0;///    int maxval=-inf,minval=inf,val;    for(int st=state;st;st-=st&(-st))     {        int i=st&(-st),x=rpw2[i];        g[pt[x].x][pt[x].y]=(who==0?'x':'o');        if(check(pt[x].x,pt[x].y)) val=who^1;        else val=maxmin(state-i,who^1,now+(who+1)*pw3[x],maxval,minval,dep+1);        g[pt[x].x][pt[x].y]='.';        if(who==0&&val>beta) return val;        if(who==1&&val<alpha) return val;        maxval=max(maxval,val);        minval=min(minval,val);        if(dep==0&&val>0) {            if(ansx>pt[x].x||(ansx==pt[x].x&&ansy>pt[x].y)) {                ansx=pt[x].x; ansy=pt[x].y;            }        }    }    if(who==0) return hash[now]=maxval;    else return hash[now]=minval;}void init() {    pw2[0]=pw3[0]=1; rpw2.clear();    for(int i=0;i<20;i++) pw2[i]=1<<i,rpw2[1<<i]=i;    for(int i=1;i<15;i++) pw3[i]=pw3[i-1]*3;}int main(){    int i,j,t,cas=0;    int st;    char c[5];    init();    while(cin>>c)    {        if(c[0]=='$') break;        m=0;        for(i=0;i<n;i++) {            scanf("%s",g[i]);            for(j=0;j<n;j++) {                if(g[i][j]=='.')pt[m].x=i,pt[m++].y=j;            }        }        st=pw2[m]-1; //111111        //score=-inf;        for(i=0;i<pw3[m];i++) hash[i]=-inf;        ansx=inf;///        int ret=maxmin(st,0,0,-inf,inf,0);        if(ret==0) puts("#####");        else printf("(%d,%d)\n",ansx,ansy);    }    return 0;}/*401.100...01....140.010.011..0.1..0*/

 

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.