POJ 1568 Find the Winning Move

2021-04-01 08:25

阅读:756

标签:max   move   ref   char   剪枝   get   输出   The   代码   

Find the Winning Move

链接

 

题意:

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

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

 

分析:

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

 

代码:

 1 #include 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; i4; ++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; i4; ++i) {
19         int ra = 0,rb = 0; //
20         int ca = 0,cb = 0; //
21         for (int j=1; j4; ++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; i4; ++i) 
38             for (int j=1; j4; ++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; i4; ++i) 
50             for (int j=1; j4; ++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  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; i4; ++i) {
67             scanf("%s",g[i]+1);
68             for (int j=1; j4; ++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 }

 

POJ 1568 Find the Winning Move

标签:max   move   ref   char   剪枝   get   输出   The   代码   

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


评论


亲,登录后才可以留言!