【POJ1568】【极大极小搜索+alpha-beta剪枝】Find the Winning Move
Description
Assuming that it is x's turn to move, x is said to have a forced win
if x can make a move such that no matter what moves o makes for the
rest of the game, x can win. This does not necessarily mean that x will
win on the very next move, although that is a possibility. It means that
x has a winning strategy that will guarantee an eventual victory
regardless of what o does.
Your job is to write a program that, given a partially-completed
game with x to move next, will determine whether x has a forced win. You
can assume that each player has made at least two moves, that the game
has not already been won by either player, and that the board is not
full.
Input
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 is followed by four lines
representing the board; formatting is exactly as shown in the example.
The characters used in a board description are the period (representing
an empty space), lowercase x, and lowercase o. For each test case,
output a line containing the (row, column) position of the first forced
win for x, or '#####' if there is no forced win. Format the output
exactly as shown in the example.
Output
this problem, the first forced win is determined by board position, not
the number of moves required for victory. Search for a forced win by
examining positions (0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), ...,
(3, 2), (3, 3), in that order, and output the first forced win you
find. In the second test case below, note that x could win immediately
by playing at (0, 3) or (2, 0), but playing at (0, 1) will still ensure
victory (although it unnecessarily delays it), and position (0, 1) comes
first.
Sample Input
?
....
.xo.
.ox.
....
?
o...
.ox.
.xxx
xooo
$
Sample Output
#####
(0,1)
Source
/*
宋代仲殊
《南柯子·十里青山远》
十里青山远,潮平路带沙。数声啼鸟怨年华。又是凄凉时候,在天涯。
白露收残月,清风散晓霞。绿杨堤畔问荷花:记得年时沽酒,那人家?
*/
#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define LOCAL
const int MAXL = ;
const long long MOD = ;
const int MAXK = + ;
const int MAXN = + ;
const int MAXM = ;
const int INF = 0x7fffffff;
using namespace std;
char str[][];
int tot, X, Y;//代表棋子的数量 void init(){
tot = ;
for (int i = ; i <= ; i++){
scanf("%s", str[i] + );
for (int j = ; j <= ; j++) if (str[i][j] != '.') tot++;
}
/*for (int i = 1; i <= 4; i++){
for (int j = 1; j <= 4; j++)
printf("%c", str[i][j]);
printf("\n");
}*/
}
//判断是否达成胜利条件
bool check(int x, int y){
int num = ;//判断棋子的个数
for (int i = ;i <= ; i++){
//注意不要全部判断了
if (str[x][i] == 'o') num++;
else if (str[x][i] == 'x') num--;
}
if (num == || num == -) return ;
num = ;
for (int i = ; i <= ; i++){
if (str[i][y] == 'o') num++;
else if (str[i][y] == 'x') num--;
}
if (num == || num == -) return ;
num = ;
//判断主对角线
for (int i = ; i <= ; i++){
if (str[i][i] == 'o') num++;
else if (str[i][i] == 'x') num--;
}
if (num == || num == -) return ;
num = ;
for (int i = ; i <= ; i++){
if (str[i][ - i + ] == 'o') num++;
else if (str[i][ - i + ] == 'x') num--;
}
if (num == || num == -) return ;
return ;
}
int MaxSearch(int x, int y, int alpha);
int MinSearch(int x, int y, int beta); int MaxSearch(int x, int y, int alpha){
int Ans = -INF;
//最大获益
if (check(x, y)) return Ans;
if (tot == ) return ; for (int i = ; i <= ; i++)
for (int j = ; j <= ; j++){
if (str[i][j] != '.') continue;
str[i][j] = 'x';tot++;
int tmp = MinSearch(i, j, Ans);
str[i][j] = '.';tot--;
Ans = max(Ans, tmp);
if (Ans >= alpha) return Ans;
}
return Ans;
}
int MinSearch(int x, int y, int beta){
int Ans = INF;
//最小损失
if (check(x, y)) return Ans;
if (tot == ) return ;//平局 for (int i = ; i <= ; i++)
for (int j = ; j <= ; j++){
if (str[i][j] != '.') continue;
str[i][j] = 'o'; tot++;
int tmp = MaxSearch(i, j, Ans);
str[i][j] = '.'; tot--;
Ans = min(Ans , tmp);
if (Ans <= beta) return Ans;
}
return Ans;
}
bool dfs(){
int beta = -INF;
for (int i = ; i <= ; i++)
for (int j = ; j <= ; j++){
if (str[i][j] != '.') continue;
str[i][j] = 'x'; tot++;
int tmp = MinSearch(i, j, beta);
str[i][j] = '.'; tot--;
beta = max(beta, tmp);
if (beta == INF){
X = i;Y = j;
return ;
}
}
return ;
} int main () { char ch[];
while (scanf("%s", ch)!= EOF && ch[] != '$'){
init();
if (dfs()) printf("(%d,%d)\n", X - , Y - );//代表终点坐标
else printf("#####\n");
}
return ;
}
【POJ1568】【极大极小搜索+alpha-beta剪枝】Find the Winning Move的更多相关文章
- 算法笔记--极大极小搜索及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领域里非常经典的算法,它使用深度优先搜索(因为限制最大层数,所以也可以称为迭代加深搜索) ...
- poj1568 Find the Winning Move[极大极小搜索+alpha-beta剪枝]
Find the Winning Move Time Limit: 3000MS Memory Limit: 32768K Total Submissions: 1286 Accepted: ...
- POJ 1568 极大极小搜索 + alpha-beta剪枝
极小极大搜索 的个人理解(alpha-beta剪枝) 主要算法依据就是根据极大极小搜索实现的. 苦逼的是,查了两个晚上的错,原来最终是判断函数写错了..瞬间吐血! ps. 据说加一句 if sum & ...
- 极大极小搜索思想+(α/β)减枝 【转自-----https://blog.csdn.net/hzk_cpp/article/details/79275772】
极大极小搜索,即minimax搜索算法,专门用来做博弈论的问题的暴力. 多被称为对抗搜索算法. 这个搜索算法的基本思想就是分两层,一层是先手,记为a,还有一层是后手,记为b. 这个搜索是认为这a与b的 ...
- 博弈论经典算法(一)——对抗搜索与Alpha-Beta剪枝
前言 在一些复杂的博弈论题目中,每一轮操作都可能有许多决策,于是就会形成一棵庞大的博弈树. 而有一些博弈论题没有什么规律,针对这样的问题,我们就需要用一些十分玄学的算法. 例如对抗搜索. 对抗搜索简介 ...
- 【迭代博弈+搜索+剪枝】poj-1568--Find the Winning Move
poj 1568:Find the Winning Move [迭代博弈+搜索+剪枝] 题面省略... Input The input contains one or more test cas ...
- poj 1568 Find the Winning Move 极大极小搜索
思路:用极大极小搜索解决这样的问题很方便!! 代码如下: #include <cstdio> #include <algorithm> #define inf 10000000 ...
- 软件发布版本区别介绍-Alpha,Beta,RC,Release
Alpha: Alpha是内部测试版,一般不向外部发布,会有很多Bug.除非你也是测试人员,否则不建议使用. 是希腊字母的第一位,表示最初级的版本 alpha就是α,beta就是β alpha版就是比 ...
- 软工+C(2017第4期) Alpha/Beta换人
// 上一篇:超链接 // 下一篇:工具和结构化 注:在一次软件工程讨论课程进度设计的过程中,出现了这个关于 Alpha/Beta换人机制的讨论,这个机制在不同学校有不同的实施,本篇积累各方观点,持续 ...
随机推荐
- Xmanager连接CentOS的远程桌面
本文主要介绍通过Xmanager连接CentOS远程桌面时,在CentOS系统上需要做的一些配置. 1. Xmanager简介 Xmanager是一个运行于 Windows平台上的高性能的X Serv ...
- 数据库连接&数据库进程&数据库操作
root@webwall:/home/xiachengjiao# vi/webwall/mysql/my.cnf(看配置文件中的参数) root@webwall:/webwall/mysql/bin# ...
- UVA 11427 Expect the Expected(DP+概率)
链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35396 [思路] DP+概率 见白书. [代码] #include&l ...
- UVa10047 The Monocycle
UVa10047 The Monocycle 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19491 (以上摘自htt ...
- [转]C语言单引号和双引号的区别
单引号和双引号在C中的意义完全不同,包围在单引号中的一个字符只是编写整数的另一种方法.这个整数是给定的字符在实现的对照序列中的一个对应的值,即ASCII码值.因此在一个ASCII实现中,‘a’和014 ...
- LayoutInflater作用及使用--自定义EditText,自带清除内容按钮
作用: 1.对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入. 2.对于一个已经载入的Activity, 就可以使用实现了这个Activiyt的的findViewById方法 ...
- 挣值管理不是搞数字游戏(3)——进阶指标:CV、SV、CPI、SPI、EAC
摘要: 要考PMP(Project Management Professional ),挣值管理是必考的知识.软件项目有很大的特殊性,不少人认为挣值管理不太适用于软件项目.挣值管理相关资料也比较超多, ...
- 月見(つきみ) 夕(ゆう) SumiHui.墨虺
造影早迎中秋,自己通过word设计的
- pomelo 服务器之间的通信
master服务器在启动的时候会启动mater服务,生成一个MasterAgent,作为中心服务器. 然后所有服务器,包括mater服务器,都会启动monitor服务,生成一个MonitorAgent ...
- SQL string类型的数据按int类型排序 分类: SQL Server 2014-12-08 16:56 393人阅读 评论(0) 收藏
说明: 我在做wms进销存软件时,发现一个问题:一张入库单(T_OutIn_BoxTop),入库扫描时要分成多箱,箱号(BoxTop_No)可以是数字也可以是字符串,所以箱号只能是字符串类型的,问题来 ...