程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj1568 Find the Winning Move----Find the Winning Move極大極小搜索

poj1568 Find the Winning Move----Find the Winning Move極大極小搜索

編輯:C++入門知識

ind the Winning Move
Time Limit: 3000MS   Memory Limit: 32768K
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 victory 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 victory. 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 could win immediately by playing at (0, 3) or (2, 0), but playing at (0, 1) will still ensure victory (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
代碼參考的這http://blog.csdn.net/lencle/article/details/7088034
金華邀請賽那題或許就源自這題了。
[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)正無窮,先手必勝 
            } 
    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)負無窮,後手必勝 
            } 
    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)正無窮,先手必勝 
             } 
     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) {  //強力剪枝 
                      printf("#####\n"); 
                      continue; 
        } 
        if (tryit()) printf("(%d,%d)\n",xi,xj); 
        else printf("#####\n"); 
    } 
    return 0; 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved