Poj1568 Find the Winning Move ---- Find the Winning Move extremely small search

Source: Internet
Author: User

Ind 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 the 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.
. Xxx
Xooo
$
Sample Output
#####
(0, 1)
Source
Mid-Central USA 1999
This http://blog.csdn.net/lencle/article/details/7088034 that the code references
The question of the Jinhua Invitational competition may come from this question.
[Cpp]
# Include <iostream>
# Include <memory. h>
# Include <stdio. h>
Using namespace std;
# Define inf 100000000
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 maxfind (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) return 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, the latter wins
}
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) is infinite, 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;
}

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.