POJ 1568 极大极小搜索 + alpha-beta剪枝
极小极大搜索 的个人理解(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剪枝的更多相关文章
- 算法笔记--极大极小搜索及alpha-beta剪枝
参考1:https://www.zhihu.com/question/27221568 参考2:https://blog.csdn.net/hzk_cpp/article/details/792757 ...
- 新手立体四子棋AI教程(3)——极值搜索与Alpha-Beta剪枝
上一篇我们讲了评估函数,这一篇我们来讲讲立体四子棋的搜索函数. 一.极值搜索 极值搜索是game playing领域里非常经典的算法,它使用深度优先搜索(因为限制最大层数,所以也可以称为迭代加深搜索) ...
- poj 1568 Find the Winning Move 极大极小搜索
思路:用极大极小搜索解决这样的问题很方便!! 代码如下: #include <cstdio> #include <algorithm> #define inf 10000000 ...
- 极大极小搜索思想+(α/β)减枝 【转自-----https://blog.csdn.net/hzk_cpp/article/details/79275772】
极大极小搜索,即minimax搜索算法,专门用来做博弈论的问题的暴力. 多被称为对抗搜索算法. 这个搜索算法的基本思想就是分两层,一层是先手,记为a,还有一层是后手,记为b. 这个搜索是认为这a与b的 ...
- POJ 1568 Find the Winning Move
Find the Winning Move 链接 题意: 4*4的棋盘,给出一个初始局面,问先手有没有必胜策略? 有的话输出第一步下在哪里,如果有多个,按(0, 0), (0, 1), (0, 2), ...
- 博弈论经典算法(一)——对抗搜索与Alpha-Beta剪枝
前言 在一些复杂的博弈论题目中,每一轮操作都可能有许多决策,于是就会形成一棵庞大的博弈树. 而有一些博弈论题没有什么规律,针对这样的问题,我们就需要用一些十分玄学的算法. 例如对抗搜索. 对抗搜索简介 ...
- 软件发布版本区别介绍-Alpha,Beta,RC,Release
Alpha: Alpha是内部测试版,一般不向外部发布,会有很多Bug.除非你也是测试人员,否则不建议使用. 是希腊字母的第一位,表示最初级的版本 alpha就是α,beta就是β alpha版就是比 ...
- 软工+C(2017第4期) Alpha/Beta换人
// 上一篇:超链接 // 下一篇:工具和结构化 注:在一次软件工程讨论课程进度设计的过程中,出现了这个关于 Alpha/Beta换人机制的讨论,这个机制在不同学校有不同的实施,本篇积累各方观点,持续 ...
- 软工+C(4): Alpha/Beta换人
// 上一篇:超链接 // 下一篇:工具和结构化 注:在一次软件工程讨论课程进度设计的过程中,出现了这个关于 Alpha/Beta换人机制的讨论,这个机制在不同学校有不同的实施,本篇积累各方观点,持续 ...
随机推荐
- yii2引入js和css
assets/AppAsset.php public $css = [ 'css/site.css', 'css/font/css/font-awesome.min.css', 'css/doc.cs ...
- C# 导出 Excel 的各种方法总结
第一种:使用 Microsoft.Office.Interop.Excel.dll 首先需要安装 office 的 excel,然后再找到 Microsoft.Office.Interop.Excel ...
- [笔记] 基于nvidia/cuda的深度学习基础镜像构建流程
基于NVidia开源的nvidia/cuda image,构建适用于DeepLearning的基础image. 思路就是先把常用的东西都塞进去,再装某个框架就省事儿了. 为了体验重装系统的乐趣,所以采 ...
- 关于js的异常
遇到异常,通常会有两种处理办法1.处理异常 try{ //可能出现异常的代码 }catch(e){ //处理异常 } 2.抛出异常 public void getName throws Excepti ...
- Winter-2-STL-C Where is the Marble? 解题报告及测试数据
Time Limit:3000MS Memory Limit:0KB Description Download as PDF Raju and Meena love to play with ...
- android 如何获取当前的Activity类名
比如友盟统计页面停留时间,咱们需要知道当前页面停留了多久. 一般我们都有一个父类Activity,用下面的方法可以获得完整的包名.类名结构 this.getLocalClassName() 输出如下: ...
- 【WPF】修改ComboBox样式
修改WPF默认的ComboBox控件样式 如下图所示: 修改代码如下: <UserControl.Resources> <Style TargetType="ToggleB ...
- 解读:MultipleOutputs类
//MultipleOutputs类用于简化多文件输出The MultipleOutputs class simplifies writing output data to multiple outp ...
- cogs 341:[NOI2005] 聪聪与可可
★★ 输入文件:cchkk.in 输出文件:cchkk.out 简单对比 时间限制:1 s 内存限制:256 MB [问题描述] 在一个魔法森林里,住着一只聪明的小猫聪聪和一只可爱的小 ...
- redis命令的使用
批量删除特定前缀的keys redis-cli KEYS "prefix:*" | xargs redis-cli DEL 返回list的长度 > LPUSH test &q ...