poj1568 Find the Winning Move[极大极小搜索+alpha-beta剪枝]
| Time Limit: 3000MS | Memory Limit: 32768K | |
| Total Submissions: 1286 | Accepted: 626 |
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
Output
Sample Input
?
....
.xo.
.ox.
....
?
o...
.ox.
.xxx
xooo
$
Sample Output
#####
(0,1)
Source


#include<cstdio>
using namespace std;
char s[][];
int chess,X,Y;
inline int abs(int x){return x>?x:-x;}
bool check(int x,int y){//判断一个局面是否结束
int tot=;
for(int i=;i<;i++) s[x][i]=='o'?tot++:s[x][i]=='x'?tot--:tot;//横向判断
if(abs(tot)==) return ;tot=;
for(int i=;i<;i++) s[i][y]=='o'?tot++:s[i][y]=='x'?tot--:tot;//纵向判断
if(abs(tot)==) return ;tot=;
for(int i=;i<;i++) s[i][i]=='o'?tot++:s[i][i]=='x'?tot--:tot;//正对角线判断
if(abs(tot)==) return ;tot=;
for(int i=;i<;i++) s[i][-i]=='o'?tot++:s[i][-i]=='x'?tot--:tot;//反对角线判断
if(abs(tot)==) return ;
return ;
}
int Min(int ,int);
int Max(int ,int);
int Max(int x,int y){
if(check(x,y)) return -;//已经结束(对方胜)
if(chess==) return ;//平局
for(int i=,now;i<;i++){
for(int j=;j<;j++){
if(s[i][j]=='.'){
s[i][j]='x';chess++;
now=Min(i,j);
s[i][j]='.';chess--;
//对方需要找的最差估价,如果当前比之前最差的高,α剪枝
if(now==) return ;
}
}
}
return -;
}
int Min(int x,int y){
if(check(x,y)) return ;//已经结束(己方胜)
if(chess==) return ;
for(int i=,now;i<;i++){
for(int j=;j<;j++){
if(s[i][j]=='.'){
s[i][j]='o';chess++;
now=Max(i,j);
s[i][j]='.';chess--;
//自己需要找的最高估价,如果当前比之前最差的低,β剪枝
if(!now||now==-) return -;
}
}
}
return ;
}
bool solve(){
for(int i=,now;i<;i++){
for(int j=;j<;j++){//枚举,然后搜索
if(s[i][j]=='.'){
s[i][j]='x';chess++;
now=Min(i,j);
s[i][j]='.';chess--;
if(now==){
X=i;Y=j;
return ;
}
}
}
}
return ;
}
int main(){
char ch[];
while(~scanf("%s",ch)&&ch[]=='?'){
for(int i=;i<;i++) scanf("%s",s[i]);chess=;
for(int i=;i<;i++) for(int j=;j<;j++) chess+=s[i][j]!='.';
if(chess<=){puts("#####");continue;}//一定平局(对方都绝顶聪明的话)
if(solve()) printf("(%d,%d)\n",X,Y);
else puts("#####");
}
return ;
}
poj1568 Find the Winning Move[极大极小搜索+alpha-beta剪枝]的更多相关文章
- poj 1568 Find the Winning Move 极大极小搜索
思路:用极大极小搜索解决这样的问题很方便!! 代码如下: #include <cstdio> #include <algorithm> #define inf 10000000 ...
- 算法笔记--极大极小搜索及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
http://poj.org/problem?id=1568 (题目链接) 题意 两人下4*4的井字棋,给出一个残局,问是否有先手必胜策略. Solution 极大极小搜索.. 这里有个强力优化,若已 ...
- 【迭代博弈+搜索+剪枝】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
Find the Winning Move 链接 题意: 4*4的棋盘,给出一个初始局面,问先手有没有必胜策略? 有的话输出第一步下在哪里,如果有多个,按(0, 0), (0, 1), (0, 2), ...
- 极大极小搜索思想+(α/β)减枝 【转自-----https://blog.csdn.net/hzk_cpp/article/details/79275772】
极大极小搜索,即minimax搜索算法,专门用来做博弈论的问题的暴力. 多被称为对抗搜索算法. 这个搜索算法的基本思想就是分两层,一层是先手,记为a,还有一层是后手,记为b. 这个搜索是认为这a与b的 ...
- POJ 1568 极大极小搜索 + alpha-beta剪枝
极小极大搜索 的个人理解(alpha-beta剪枝) 主要算法依据就是根据极大极小搜索实现的. 苦逼的是,查了两个晚上的错,原来最终是判断函数写错了..瞬间吐血! ps. 据说加一句 if sum & ...
- [CodeVs3196]黄金宝藏(DP/极大极小搜索)
题目大意:给出n(≤500)个数,两个人轮流取数,每次可以从数列左边或者右边取一个数,直到所有的数被取完,两个人都以最优策略取数,求最后两人所得分数. 显然这种类型的博弈题,第一眼就是极大极小搜索+记 ...
随机推荐
- Phalcon的MVC框架解析
1. mvc/simple从最简单的入手吧. 把一些能及时说明白的东西写在注释里了,需要扩展的知识列在下面. public/index.php <?php $loader = new \Phal ...
- LintCode - Copy List with Random Pointer
LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...
- 一个CSS3滤镜Drop-shadow阴影效果
<html> <head> <title>CSS3 Drop-shadow阴影</title> <style type="text/cs ...
- 如果通过html 链接打开app
https://my.oschina.net/liucundong/blog/354029 <!doctype html> <html> <head> <me ...
- GIt的基本知识
以前已经把git 看过一遍了,由于好久没有用它 ,现在已经忘了.现在呢,要用它进行同步代码,所以呢,我打算记一记,再复习复习. 参考文件:https://git-scm.com/book/zh/v2 ...
- javascript -- addEventListener()和removeEventListener
addEventListener()与removeEventListener()用于处理指定和删除事件处理程序操作.所有的DOM节点中都包含这两种方法,并且它们都接受3个参数:要处理的事件名.作为事件 ...
- hadoop2.7.1单机和伪集群的搭建-0
内容中包含 base64string 图片造成字符过多,拒绝显示
- jQuery实现HTML表格单元格的合并功能
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- ubuntu-14.04.2-desktop-amd64.iso:ubuntu-14.04.2-desktop-amd64:安装Oracle11gR2
ubuntu 桌面版的安装不介绍. 如何安装oracle:核心步骤和关键点. ln -sf /bin/bash /bin/sh ln -sf /usr/bin/basename /bin/basena ...
- 怎么用MathType输入对数函数
MathType是一款强大的公式编辑软件,但是一些新手用户对于其应用还不是那么熟练,很多的操作不是很精通.比如,怎么用MathType输入对数函数.下面就给大家介绍介绍MathType对数函数的输入方 ...