极小极大搜索 的个人理解(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. NHibernate和Castle调试过程中,如何输出SQL的问题

             首先,我在此需要强调的是,不管是Castle或者NHibernate输出SQL,都应该是属于NHibernate的技术,Castle的本身也是基于NHibernate开发的ORM框架 ...

  2. 基于bootstrap metronic-responsive-admin-dashboard-template 开发管理后台

    简单介绍 我们这个系统是基于bootstrap metronic-responsive-admin-dashboard-template 这个模板开发的.版本用的是metronic_v4.5.2 效果 ...

  3. JavaScript-dom4 date string 事件绑定

    内置date <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  4. Linux_Vi_命令

    Linux Vi 命令 ************************************************************************* 在vi中使用命令的方法是:冒 ...

  5. XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem D. Clones and Treasures

    题目:Problem D. Clones and TreasuresInput file: standard inputOutput file: standard outputTime limit: ...

  6. Ubuntu16.04双网卡配置,内网外网同时访问

    Ubuntu16.04双网卡配置,内网外网同时访问 配置:vim/etc/network/interface auto lo iface lo inet loopback auto eno1 ifac ...

  7. c++对txt文件的读取与写入

    转自:http://blog.csdn.net/lh3325251325/article/details/4761575 #include <iostream> #include < ...

  8. 【c++ primer, 5e】函数声明 & 分离式编译

    p186~p188: 函数声明1.函数只能定义一次,但是可以声明多次. 2.函数的接口:返回类型 + 函数名 + 形参类型 3.为什么要在头文件中进行函数声明???在源文件中定义?暂时理解到,这么做可 ...

  9. Ant Design 常用命令汇总

    Ant Design React 安装 1. 安装脚手架工具# antd-init 是一个用于演示 antd 如何使用的脚手架工具,真实项目建议使用 dva-cli. $ npm install an ...

  10. MVC的局部视图传参的小技巧--见人才网头部导航

    当我们设计一个局部视图时,当出现有类似导航的功能(如:选择左边的某个按钮跳到某个页,且顶部导航也作相印改变),如果我们选择把导航作为局部视图来处理,调用就可以做如下处理: @Html.RenderAc ...