极小极大搜索 的个人理解(alpha-beta剪枝)

主要算法依据就是根据极大极小搜索实现的。

苦逼的是,查了两个晚上的错,原来最终是判断函数写错了。。瞬间吐血!

ps. 据说加一句 if sum < 4 printf("#####\n")会变态的快,不过懒得加了

ps. 1表示胜利,0表示平局,-1表示失败。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <utility>
#include <vector>
#include <queue>
#include <set>
#define INF 0x3f3f3f3f using namespace std; int map[4][4], sum, winx, winy;
char c; bool check(int otherplayer)
{
for(int i = 0; i < 4; i ++)
{
int tmp = 0;
for(int j = 0; j < 4; j ++)
tmp += (map[i][j] == otherplayer);
if(tmp == 4)
return 1;
}
for(int j = 0; j < 4; j ++)
{
int tmp = 0;
for(int i = 0; i < 4; i ++)
tmp += (map[i][j] == otherplayer);
if(tmp == 4)
return 1;
}
int tmp1 = 0, tmp2 = 0;
for(int i = 0; i < 4; i ++)
{
tmp1 += (map[i][i] == otherplayer);
tmp2 += (map[3-i][i] == otherplayer);
}
if(tmp1 == 4 || tmp2 == 4)
return 1;
return 0;
} int alpha_beta(int depth, int alpha, int beta)
{
int nowplayer = depth&1;
if(check(1-nowplayer))
return -1;
if(sum == 16)
return 0;
for(int i = 0; i < 4; i ++)
for(int j = 0; j < 4; j ++)
if(map[i][j] == 2)
{
map[i][j] = nowplayer;
sum ++;
int tmp = -alpha_beta(depth+1, -beta, -alpha);
map[i][j] = 2;
sum --;
if(tmp >= beta)
return beta;
if(tmp > alpha)
alpha = tmp;
if(depth == 0 && alpha == 1)
{
winx = i, winy = j;
return alpha;
}
}
return alpha;
} int main()
{
while(getchar() != '$')
{
sum = 0;
getchar();
for(int i = 0; i < 4; i ++)
for(int j = 0; j < 4; j ++)
{
c = getchar();
switch (c){
case '.':
map[i][j] = 2;
break;
case 'x':
map[i][j] = 0;
sum ++;
break;
case 'o':
map[i][j] = 1;
sum ++;
}
if(j == 3) getchar();
}
if(alpha_beta(0, -INF, INF) == 1)
printf("(%d,%d)\n", winx, winy);
else
printf("#####\n");
}
return 0;
} /* ?
....
.xo.
.ox.
....
?
o...
.ox.
.xxx
xooo
?
xxxo
oxox
....
xxxo
$ */

POJ 1568 极大极小搜索 + alpha-beta剪枝的更多相关文章

  1. 算法笔记--极大极小搜索及alpha-beta剪枝

    参考1:https://www.zhihu.com/question/27221568 参考2:https://blog.csdn.net/hzk_cpp/article/details/792757 ...

  2. 新手立体四子棋AI教程(3)——极值搜索与Alpha-Beta剪枝

    上一篇我们讲了评估函数,这一篇我们来讲讲立体四子棋的搜索函数. 一.极值搜索 极值搜索是game playing领域里非常经典的算法,它使用深度优先搜索(因为限制最大层数,所以也可以称为迭代加深搜索) ...

  3. poj 1568 Find the Winning Move 极大极小搜索

    思路:用极大极小搜索解决这样的问题很方便!! 代码如下: #include <cstdio> #include <algorithm> #define inf 10000000 ...

  4. 极大极小搜索思想+(α/β)减枝 【转自-----https://blog.csdn.net/hzk_cpp/article/details/79275772】

    极大极小搜索,即minimax搜索算法,专门用来做博弈论的问题的暴力. 多被称为对抗搜索算法. 这个搜索算法的基本思想就是分两层,一层是先手,记为a,还有一层是后手,记为b. 这个搜索是认为这a与b的 ...

  5. POJ 1568 Find the Winning Move

    Find the Winning Move 链接 题意: 4*4的棋盘,给出一个初始局面,问先手有没有必胜策略? 有的话输出第一步下在哪里,如果有多个,按(0, 0), (0, 1), (0, 2), ...

  6. 博弈论经典算法(一)——对抗搜索与Alpha-Beta剪枝

    前言 在一些复杂的博弈论题目中,每一轮操作都可能有许多决策,于是就会形成一棵庞大的博弈树. 而有一些博弈论题没有什么规律,针对这样的问题,我们就需要用一些十分玄学的算法. 例如对抗搜索. 对抗搜索简介 ...

  7. 软件发布版本区别介绍-Alpha,Beta,RC,Release

    Alpha: Alpha是内部测试版,一般不向外部发布,会有很多Bug.除非你也是测试人员,否则不建议使用. 是希腊字母的第一位,表示最初级的版本 alpha就是α,beta就是β alpha版就是比 ...

  8. 软工+C(2017第4期) Alpha/Beta换人

    // 上一篇:超链接 // 下一篇:工具和结构化 注:在一次软件工程讨论课程进度设计的过程中,出现了这个关于 Alpha/Beta换人机制的讨论,这个机制在不同学校有不同的实施,本篇积累各方观点,持续 ...

  9. 软工+C(4): Alpha/Beta换人

    // 上一篇:超链接 // 下一篇:工具和结构化 注:在一次软件工程讨论课程进度设计的过程中,出现了这个关于 Alpha/Beta换人机制的讨论,这个机制在不同学校有不同的实施,本篇积累各方观点,持续 ...

随机推荐

  1. 取MAC地址 (含多网卡),最好的方法,支持Vista,Win7

    取MAC地址 (含多网卡),最好的方法,支持Vista,Win7 unit Unit1; interface usesWindows, Messages, SysUtils, Variants, Cl ...

  2. android 读取通讯录显示到gridview

    ........... <GridView android:id="@+id/gridView1" android:layout_width="match_pare ...

  3. ruby中的可调用对象--proc和lamdba

    ruby中将块转变成对象的三种方法 ruby中的大部分东西都是对象,但是块不是.那么,如果你想存下来一个块,方便以后使用,你就需要一个对象.ruby中有三种方法,把块转换成可以利用的对象. Proc. ...

  4. 235. Lowest Common Ancestor of a Binary Search Tree(LCA最低公共祖先)

      Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the ...

  5. cdoj1588 潘爷泡妹

    地址:http://acm.uestc.edu.cn/#/problem/show/1588 题目:复制过来发现有问题,自己去cdoj看吧 思路: 1.先进行多次spfa跑出所有人之间的相互到达所需要 ...

  6. python 实现3-2 问候语: 继续使用练习 3-1 中的列表,但不打印每个朋友的姓名,而为每人打印一条消息。每条消息都包含相同的问候语,但抬头为相应朋友的姓名。

    names = ['linda', 'battile', 'emly'] print(names[0].title() + " " + "good moning!&quo ...

  7. OpenStack之Neutron网络服务(一)

    1.Neutron概要 OpenStack网络服务提供了一个API接口,允许用户在云上设置和定义网络连接和地址.这个网络服务的项目代码名称是Neutron.OpenStack网络处理虚拟设备的创建和管 ...

  8. MVC通过服务端对数据进行验证(和AJAX验证一样)

    在实体类中 添加 Remote属性,指定用某个View下的某个方法进行验证,如下面表示用User控制器中的UserExiting方法验证 public    class   User { [Remot ...

  9. 20145310 《Java程序设计》第6周学习总结

    20145310 <Java程序设计>第6周学习总结 教材学习内容总结 本周主要进行第十章和第十一章的学习. 第十章 Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流 ...

  10. Ubuntu 12.04下安装QQ 2012 Beta3

    Ubuntu 12.04下安装QQ 2012 Beta3   由于wine的发展非常迅速.现在网上的利用老版本的wine来安装QQ2012的教程已经有些过时了.实际上操作起来非常简单: 第一步:Ctr ...