您的位置:首页 > 其它

【POJ1568】【极大极小搜索+alpha-beta剪枝】Find the Winning Move

2015-03-22 19:28 465 查看
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
【分析】
经典的题目,第一次写。。参考了别人的代码

/*
宋代仲殊
《南柯子·十里青山远》
十里青山远,潮平路带沙。数声啼鸟怨年华。又是凄凉时候,在天涯。
白露收残月,清风散晓霞。绿杨堤畔问荷花:记得年时沽酒,那人家?
*/
#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define LOCAL
const int MAXL = 20;
const long long MOD = 1000000007;
const int MAXK = 10000 + 10;
const int MAXN = 35000 + 10;
const int MAXM = 35;
const int INF = 0x7fffffff;
using namespace std;
char str[10][10];
int tot, X, Y;//代表棋子的数量

void init(){
tot = 0;
for (int i = 1; i <= 4; i++){
scanf("%s", str[i] + 1);
for (int j = 1; j <= 4; j++) if (str[i][j] != '.') tot++;
}
/*for (int i = 1; i <= 4; i++){
for (int j = 1; j <= 4; j++)
printf("%c", str[i][j]);
printf("\n");
}*/
}
//判断是否达成胜利条件
bool check(int x, int y){
int num = 0;//判断棋子的个数
for (int i = 1;i <= 4; i++){
//注意不要全部判断了
if (str[x][i] == 'o') num++;
else if (str[x][i] == 'x') num--;
}
if (num == 4 || num == -4) return 1;
num = 0;
for (int i = 1; i <= 4; i++){
if (str[i][y] == 'o') num++;
else if (str[i][y] == 'x') num--;
}
if (num == 4 || num == -4) return 1;
num = 0;
//判断主对角线
for (int i = 1; i <= 4; i++){
if (str[i][i] == 'o') num++;
else if (str[i][i] == 'x') num--;
}
if (num == 4 || num == -4) return 1;
num = 0;
for (int i = 1; i <= 4; i++){
if (str[i][4 - i + 1] == 'o') num++;
else if (str[i][4 - i + 1] == 'x') num--;
}
if (num == 4 || num == -4) return 1;
return 0;
}
int MaxSearch(int x, int y, int alpha);
int MinSearch(int x, int y, int beta);

int MaxSearch(int x, int y, int alpha){
int Ans = -INF;
//最大获益
if (check(x, y)) return Ans;
if (tot == 16) return 0;

for (int i = 1; i <= 4; i++)
for (int j = 1; j <= 4; j++){
if (str[i][j] != '.') continue;
str[i][j] = 'x';tot++;
int tmp = MinSearch(i, j, Ans);
str[i][j] = '.';tot--;
Ans = max(Ans, tmp);
if (Ans >= alpha) return Ans;
}
return Ans;
}
int MinSearch(int x, int y, int beta){
int Ans = INF;
//最小损失
if (check(x, y)) return Ans;
if (tot == 16) return 0;//平局

for (int i = 1; i <= 4; i++)
for (int j = 1; j <= 4; j++){
if (str[i][j] != '.') continue;
str[i][j] = 'o'; tot++;
int tmp = MaxSearch(i, j, Ans);
str[i][j] = '.'; tot--;
Ans = min(Ans , tmp);
if (Ans <= beta) return Ans;
}
return Ans;
}
bool dfs(){
int beta = -INF;
for (int i = 1; i <= 4; i++)
for (int j = 1; j <= 4; j++){
if (str[i][j] != '.') continue;
str[i][j] = 'x'; tot++;
int tmp = MinSearch(i, j, beta);
str[i][j] = '.'; tot--;
beta = max(beta, tmp);
if (beta == INF){
X = i;Y = j;
return 1;
}
}
return 0;
}

int main () {

char ch[2];
while (scanf("%s", ch)!= EOF && ch[0] != '$'){
init();
if (dfs()) printf("(%d,%d)\n", X - 1, Y - 1);//代表终点坐标
else printf("#####\n");
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: