Ccf201803-4 chess evaluation, anti-search, Minimax algorithm

Source: Internet
Author: User

First of all, the minimax algorithm is to give an evaluation value to all the states that may appear, and two players decide how to play chess by calculating different chess strategies corresponding to different evaluation values. For Tic-tac-Chess games, its game tree (a variety of combinations formed by the combination of trees) is as follows:

Alice (MAX) under X,bob (MIN) under O until the end of the tree is reached a player occupies one row, and a column, a diagonal, or all squares are filled. Utility refers to the utility function, which defines the value of the player under State S. In this problem, it means:

-For Alice has won the situation, the evaluation score is (the board of empty lattice number +1);
-For the situation where Bob has won, the evaluation score is-(+1 empty squares on the board);
-For a draw, the assessment is divided into 0;

Therefore, in the strategy tree, regardless of the current situation, Alice (max) always chooses the largest evaluation of the corresponding method, Bob (min) will always choose the smallest evaluation of the corresponding method. This will enable you to win the game as soon as possible (this is the key, to be clear). The calculation method of the evaluation points of the leaf nodes in the Strategy tree (winning, losing or draw evaluation method) is given only in the topic, how to calculate the evaluation score of each non-leaf node in the strategy tree?

The answer is to use the depth-first search for the entire strategy tree to post-sequence traversal, so that the evaluation of the leaf node in the policy tree, the evaluation of the non-leaf node on the top of the calculation, and finally, the entire strategy tree will be evaluated, so you can determine how the player should be moves in the current situation.

According to the above ideas:

#include <bits/stdc++.h>using namespacestd;intq[Ten];intCheckok () {intI1, i2, OK =0;  for(I1 =1; I1 <=3; i1++) {I2=3* (I1-1); if((q[i1] = = Q[i1 +3]) && (Q[i1 +3] = = Q[i1 +6]) && (Q[I1]! =0)){            if(Q[i1] = =1) OK =1;ElseOK =2;  Break; }        if((Q[i2 +1] = = Q[i2 +2]) && (Q[i2 +2] = = Q[i2 +3]) && (Q[i2 +1] !=0)){            if(Q[i2 +1] ==1) OK =1;ElseOK =2;  Break; }    }    if((!ok) && (q[1] = = q[5]) && (q[5] = = q[9]) && (q[1] !=0)) )        if(q[1] ==1) OK =1;ElseOK =2; if((!ok) && (q[3] = = q[5]) && (q[5] = = q[7]) && (q[3] !=0))        if(q[3] ==1) OK =1;ElseOK =2; I2=0;  for(I1 =1; I1 <=9; i1++)        if(Q[i1] = =0) i2++; if(OK = =1)return(I2 +1); Else if(OK = =2)return-(I2 +1); Else if(I2 = =0)return 0; Else return  -;}intDfsintturn) {    intValue =Checkok (); if(Value! = -)returnvalue; intI1,i2; if(Turn = =1) I2 =- -; ElseI2 = -;  for(I1 =1; I1 <=9; i1++){        if(Q[I1]! =0)Continue; if(Turn = =1) {Q[I1]=1; I2= Max (I2, DFS (0)); }Else{Q[I1]=2; I2= Min (I2, DFS (1)); } Q[i1]=0; }    returnI2;}intMain () {//freopen ("A.txt", "R", stdin);    intT,i1,i2; scanf ("%d", &T);  while(t--)    {         for(I1 =1; I1 <=9; i1++) {scanf ("%d", &i2); Q[I1]=I2; } printf ("%d\n", DFS (1)); }    return 0;}

You can get a 100-point answer.

Also say is the TIC strategy tree, you can completely traverse, directly using the Minimax algorithm, in the face of more complex chess, but also need to adopt βsearch pruning and other more advanced methods.

Reference book: Artificial intelligence-a modern approach.

Ccf201803-4 chess evaluation, anti-search, Minimax algorithm

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.