POJ 1568 Find the Winning Move(极大极小搜索)
题目链接:http://poj.org/problem?id=1568
题意:给出一个4*4的棋盘,x和o两人轮流放。先放够连续四个的赢。给定一个局面,下一个轮到x放。问x是否有必胜策略?若有,输出能够赢的最小的坐标?
思路:(1)Maxfind(Min):每次放x,若得到的Max大于等于Min,则直接返回Max。因为Minfind要用这个Max更新Min,若x放置之后能够得到一个比Min大的,显然这样放最好;因为Min无法用Max更新;
(2)Minfind(Max)类似;
(3)因此,枚举第一个人放的位置,Minfind,返回的是INF则胜利。
char s[10][10];
int ansX,ansY,chessCnt;
int isOver(int x,int y)
{
int r[4]={0},c[4]={0},i,j;
clr(r,0); clr(c,0);
FOR0(i,4) FOR0(j,4)
{
if(s[i][j]=='x') r[i]++,c[j]++;
if(s[i][j]=='o') r[i]--,c[j]--;
}
if(abs(r[x])==4||abs(c[y])==4) return 1;
int cnt1=0,cnt2=0;
FOR0(i,4)
{
if(s[i][i]=='x') cnt1++;
if(s[i][3-i]=='x') cnt2++;
if(s[i][i]=='o') cnt1--;
if(s[i][3-i]=='o') cnt2--;
}
if(abs(cnt1)==4&&x==y||abs(cnt2)==4&&x+y==3) return 1;
return 0;
}
int Minfind(int,int,int);
int Maxfind(int x,int y,int Min)
{
int temp,Max=-INF,i,j;
if(isOver(x,y)) return Max;
if(chessCnt==16) return 0;
FOR0(i,4) FOR0(j,4) if(s[i][j]=='.')
{
s[i][j]='x';
chessCnt++;
temp=Minfind(i,j,Max);
chessCnt--;
s[i][j]='.';
if(temp>Max) Max=temp;
if(Max>=Min) return Max;
}
return Max;
}
int Minfind(int x,int y,int Max)
{
int temp,Min=INF,i,j;
if(isOver(x,y)) return Min;
if(chessCnt==16) return 0;
FOR0(i,4) FOR0(j,4) if(s[i][j]=='.')
{
s[i][j]='o';
chessCnt++;
temp=Maxfind(i,j,Min);
chessCnt--;
s[i][j]='.';
if(temp<Min) Min=temp;
if(Min<=Max) return Min;
}
return Min;
}
int check()
{
int Max=-INF,i,j,temp;
FOR0(i,4) FOR0(j,4) if(s[i][j]=='.')
{
s[i][j]='x';
chessCnt++;
temp=Minfind(i,j,Max);
chessCnt--;
s[i][j]='.';
if(temp>Max) Max=temp,ansX=i,ansY=j;
if(Max==INF) return 1;
}
return 0;
}
int main()
{
char op[5];
while(scanf("%s",op)&&op[0]!='$')
{
int i,j;
chessCnt=0;
FOR0(i,4)
{
RD(s[i]);
FOR0(j,4) chessCnt+=s[i][j]!='.';
}
if(chessCnt<=4||!check()) puts("#####");
else printf("(%d,%d)\n",ansX,ansY);
}
}
POJ 1568 Find the Winning Move(极大极小搜索)的更多相关文章
- poj 1568 Find the Winning Move 极大极小搜索
思路:用极大极小搜索解决这样的问题很方便!! 代码如下: #include <cstdio> #include <algorithm> #define inf 10000000 ...
- POJ 1568 Find the Winning Move
Find the Winning Move 链接 题意: 4*4的棋盘,给出一个初始局面,问先手有没有必胜策略? 有的话输出第一步下在哪里,如果有多个,按(0, 0), (0, 1), (0, 2), ...
- 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 & ...
- 【迭代博弈+搜索+剪枝】poj-1568--Find the Winning Move
poj 1568:Find the Winning Move [迭代博弈+搜索+剪枝] 题面省略... Input The input contains one or more test cas ...
- 算法笔记--极大极小搜索及alpha-beta剪枝
参考1:https://www.zhihu.com/question/27221568 参考2:https://blog.csdn.net/hzk_cpp/article/details/792757 ...
- 【poj1568】 Find the Winning Move
http://poj.org/problem?id=1568 (题目链接) 题意 两人下4*4的井字棋,给出一个残局,问是否有先手必胜策略. Solution 极大极小搜索.. 这里有个强力优化,若已 ...
- 极大极小搜索思想+(α/β)减枝 【转自-----https://blog.csdn.net/hzk_cpp/article/details/79275772】
极大极小搜索,即minimax搜索算法,专门用来做博弈论的问题的暴力. 多被称为对抗搜索算法. 这个搜索算法的基本思想就是分两层,一层是先手,记为a,还有一层是后手,记为b. 这个搜索是认为这a与b的 ...
- [CodeVs3196]黄金宝藏(DP/极大极小搜索)
题目大意:给出n(≤500)个数,两个人轮流取数,每次可以从数列左边或者右边取一个数,直到所有的数被取完,两个人都以最优策略取数,求最后两人所得分数. 显然这种类型的博弈题,第一眼就是极大极小搜索+记 ...
随机推荐
- SGU 185 Two shortest 最短路+最大流
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21068 Yesterday Vasya and Petya qua ...
- Leetcode#150 Evaluate Reverse Polish Notation
原题地址 基本栈操作. 注意数字有可能是负的. 代码: int toInteger(string &s) { ; ] == '-' ? true : false; : ; i < s.l ...
- Ubuntu 字体修改以及字体的相关知识 分类: ubuntu 2014-06-19 21:46 81人阅读 评论(0) 收藏
Ubuntu chrome 字体修改 打开任意一张含有输入框的网页,比如Google首页,然后右键点击"搜索框"会拉出一个菜单,我们这样选: 拼音检查选项==>语言设置==& ...
- Notes on the Dirichlet Distribution and Dirichlet Process
Notes on the Dirichlet Distribution and Dirichlet Process In [3]: %matplotlib inline Note: I wrote ...
- AVRStudio 的编译优化级别
-00 无优化. -01 减少代码尺寸和执行时间,不进行需要大量编译时间的优化. -02 几乎执行所有优化,而不考虑代码尺寸和执行时间. -03 执行 -02 所有的优化,以及内联函数,重命名寄存器的 ...
- AngularJs学习笔记--html compiler
原文再续,书接上回...依旧参考http://code.angularjs.org/1.0.2/docs/guide/compiler 一.总括 Angular的HTML compiler允许开发者自 ...
- My97日历控件常用功能记录
My97相信大家都不陌生,应该是我所见过的最强大的一个日历控件了,最近的项目中也比较多地用到了此控件,而且项目中经常会有不同时间范围的需求,在此列出一些比较常用的日期范围格式的设置,尽管在My97的官 ...
- ASP.NET防止用户多次登录的方法
常见的处理方法是,在用户登录时,判断此用户是否已经在Application中存在,如果存在就报错,不存在的话就加到Application中(Application是所有Session共有的,整个web ...
- 用dedecms自定义表单创建简易自助预约系统
建站往往需要根据客户的需求来增加相应的功能,比如预约.平时用比较多的是织梦系统,那么如何用dedecms自定义表单创建简易自助预约系统呢? 进入dedecms后台,左侧菜单中依次点击“核心” - 频道 ...
- main函数和启动例程
为什么汇编程序的入口是_start,而C程序的入口是main函数呢?本节就来解释这个问题.在讲例 18.1 “最简单的汇编程序”时,我们的汇编和链接步骤是: $ as hello.s -o hello ...