POJ 1568 Find the Winning Move

Find the Winning Move

链接

题意:

4*4的棋盘,给出一个初始局面,问先手有没有必胜策略?

  有的话输出第一步下在哪里,如果有多个,按(0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1) ... 的顺序输出第一个。没有输出“#####”。

分析:

  极大极小搜索,对抗搜索,α+β剪枝。

代码:

 1 #include<cstdio>
 2
 3 char g[5][5];
 4 int ansx,ansy;
 5
 6 int judge() {
 7     int za = 0,zb = 0; // 主对角线
 8     int fa = 0,fb = 0; // 副对角线
 9     for (int i=1; i<=4; ++i) {
10         if (g[i][i] == ‘x‘) za++;
11         else if (g[i][i]==‘o‘) zb++;
12         if (g[i][5-i] == ‘x‘) fa++;
13         else if (g[i][5-i]==‘o‘) fb++;
14     }
15     if (za==4 || fa==4) return 1;
16     if (zb==4 || fb==4) return -1;
17
18     for (int i=1; i<=4; ++i) {
19         int ra = 0,rb = 0; // 行
20         int ca = 0,cb = 0; // 列
21         for (int j=1; j<=4; ++j) {
22             if (g[i][j] == ‘x‘) ra++;
23             else if (g[i][j] == ‘o‘) rb++;
24             if (g[j][i] == ‘x‘) ca++;
25             else if (g[j][i] == ‘o‘) cb++;
26         }
27         if (ra == 4 || ca == 4) return 1;
28         if (rb == 4 || cb == 4) return -1;
29     }
30
31     return 0;
32 }
33 int Minimax(int player,int alpha,int beta) {
34     int res= judge();
35     if (res) return res;
36     if (player) {
37         for (int i=1; i<=4; ++i)
38             for (int j=1; j<=4; ++j)
39                 if (g[i][j] == ‘.‘) {
40                     g[i][j] = ‘x‘;
41                     res = Minimax(player^1,alpha,beta);
42                     g[i][j] = ‘.‘;
43                     if (res > alpha) alpha = res,ansx = i,ansy = j;
44                     if (alpha >= beta) return alpha;
45                 }
46         return alpha; //-
47     }
48     else {
49         for (int i=1; i<=4; ++i)
50             for (int j=1; j<=4; ++j)
51                 if (g[i][j] == ‘.‘) {
52                     g[i][j] = ‘o‘;
53                     res = Minimax(player^1,alpha,beta);
54                     g[i][j] = ‘.‘;
55                     if (res < beta) beta = res;
56                     if (alpha >= beta) return beta;
57                 }
58         return beta; //-
59     }
60 }
61
62 int main() {
63     char op[5];
64     while (scanf("%s",op) && op[0]!=‘$‘) {
65         int cnt = 0;
66         for (int i=1; i<=4; ++i) {
67             scanf("%s",g[i]+1);
68             for (int j=1; j<=4; ++j)
69                 if (g[i][j] != ‘.‘) cnt++;
70         }
71         if (cnt <= 4) {
72             puts("#####");continue;
73         }
74         int ans = Minimax(1,-1,1);
75         if (ans > 0) printf("(%d,%d)\n",ansx-1,ansy-1);
76         else puts("#####");
77     }
78     return 0;
79 }

原文地址:https://www.cnblogs.com/mjtcn/p/9240934.html

时间: 2024-10-12 02:39:47

POJ 1568 Find the Winning Move的相关文章

【迭代博弈+搜索+剪枝】poj-1568--Find the Winning Move

poj  1568:Find the Winning Move   [迭代博弈+搜索+剪枝] 题面省略... 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

poj1568 Find the Winning Move[极大极小搜索+alpha-beta剪枝]

Find the Winning Move Time Limit: 3000MS   Memory Limit: 32768K Total Submissions: 1286   Accepted: 626 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 t

POJ Find the Winning Move【minmax搜索+alpha-beta剪枝】【北大ACM/ICPC竞赛训练】

1 #include<iostream> 2 using namespace std; 3 4 int row,col,chess; 5 char board[5][5]; 6 7 int minSearch(int i,int j,int alpha); 8 int maxSearch(int i,int j,int beta); 9 10 11 bool check(int r,int c){ 12 if( board[r][0]==board[r][1] && board

【poj1568】 Find the Winning Move

http://poj.org/problem?id=1568 (题目链接) 题意 两人下4*4的井字棋,给出一个残局,问是否有先手必胜策略. Solution 极大极小搜索.. 这里有个强力优化,若已经被下了的的格子数cnt小于等于4的话,那么一定是平局至于为什么,自己YY一下发现好像是这样的.. 代码 // poj1568 #include<algorithm> #include<iostream> #include<cstring> #include<cstd

POJ 1568 极大极小搜索 + alpha-beta剪枝

极小极大搜索 的个人理解(alpha-beta剪枝) 主要算法依据就是根据极大极小搜索实现的. 苦逼的是,查了两个晚上的错,原来最终是判断函数写错了..瞬间吐血! ps. 据说加一句 if sum < 4 printf("#####\n")会变态的快,不过懒得加了 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #includ

POJ 1143 记忆化搜索+博弈论

Number Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3181   Accepted: 1280 Description Christine and Matt are playing an exciting game they just invented: the Number Game. The rules of this game are as follows. The players take tu

POJ 搜索题集

poj1010--邮票问题 DFS poj1011--Sticks dfs + 剪枝 poj1020--拼蛋糕 poj1054--The Troublesome Frog poj1062--昂贵的聘礼 poj1077--Eight poj1084--Square Destroyer poj1085--Triangle War(博弈,極大極小搜索+alpha_beta剪枝) poj1088--滑雪 poj1129--Channel Allocation 着色问题 dfs poj1154--lett

POJ 1143 Number Game 状态压缩dp

#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <stack> #include <cstdlib> #include <cmath> #include <set> #include <map> #include <vector> #include <cstri

poj 2975 Nim(博弈)

Nim Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5232   Accepted: 2444 Description Nim is a 2-player game featuring several piles of stones. Players alternate turns, and on his/her turn, a player’s move consists of removing one or mor