poj  1568:Find the Winning Move   【迭代博弈+搜索+剪枝】

题面省略。。。

Input

The 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

For 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)
#####

大致题意::

    给定一个四乘四的棋盘,面对一个残局;问先手再下一步是否可以达成最终的胜利;若可以则输出按顺序的坐标最小的那个。

解决思路:

    (我给起的名字是博弈搜索算法,其实是极大极小搜索算法!)

    博弈迭代思想+搜索+剪枝。
    思路解释起来很麻烦,简单说就是:枚举先手一步,针对新加的新手一步--然后判断是否后手无论怎么下就注定是死局......

注意:

   1、在迭代的过程中,平局也是后手胜,因为题目仅要求先手是否面对的是必赢局!
         2、我的博客专栏胡搞里也有相关的可以瞅瞅!http://www.cnblogs.com/zhazhaacmer/category/1137385.html

带注释的题解(第二种check()为超时的判断局面的函数):
 
 char mp[][];
int check(int x,int y,char mp[][]){ //判定新更改的点(x,y)是否会构成胜利的局面,两斜/横/竖
if(x==y&&mp[][]!='.'&&mp[][]==mp[][]&&mp[][]==mp[][]&&mp[][]==mp[][])
return (mp[][]=='x'?:);
if(x+y==&&mp[][]!='.'&&mp[][]==mp[][]&&mp[][]==mp[][]&&mp[][]==mp[][])
return (mp[][]=='x'?:);
if(mp[x][]!='.'&&mp[x][]==mp[x][]&&mp[x][]==mp[x][]&&mp[x][]==mp[x][])
return (mp[x][]=='x'?:);
if(mp[][y]!='.'&&mp[][y]==mp[][y]&&mp[][y]==mp[][y]&&mp[][y]==mp[][y])
return (mp[][y]=='x'?:);
}
/* !!下面这种判断是否可以达到结束的局面的方法,时间复杂度高,毕竟把整个图跑了两遍!
int check1(char mp[4][5]){ //三种返回值,0表示前者,1表示后者,-1表示平局
for(int i=0;i<=3;i++){
if(mp[i][0]!='.'&&mp[i][0]==mp[i][1]&&mp[i][1]==mp[i][2]&&mp[i][2]==mp[i][3])
return (mp[i][0]=='x'?0:1);
}
for(int i=0;i<=3;i++){
if(mp[0][i]!='.'&&mp[0][i]==mp[1][i]&&mp[1][i]==mp[2][i]&&mp[2][i]==mp[3][i])
return (mp[0][i]=='x'?0:1);
}
if(mp[0][0]!='.'&&mp[0][0]==mp[1][1]&&mp[1][1]==mp[2][2]&&mp[2][2]==mp[3][3])
return (mp[0][0]=='x'?0:1);
if(mp[0][3]!='.'&&mp[0][3]==mp[1][2]&&mp[1][2]==mp[2][1]&&mp[2][1]==mp[3][0])
return (mp[0][3]=='x'?0:1); for(int i=0;i<=3;i++){
for(int j=0;j<=3;j++){
if(mp[i][j]=='.')
return -1;
}
}
return 1;//假设整个棋盘全部布满,平局就也是后者赢!
}
*/
int check_whole(){ // //判断全局都是点'.',若是则一定是平局,此种特例不特判必定超时
for(int i=;i<=;i++){
for(int j=;j<=;j++)
if(mp[i][j]!='.')
return false;
}
return true;
}
int ansx,ansy;//记录第一次下的点的x和y坐标,也就是step=0的时候! bool dfs(int x,int y,int op,int step){//对于op来说,先手X: 0要求必赢,后手1要求不败即可!
char ch = (op==)?'x':'o'; int val=check(x,y,mp);
if(val==op)
return true;
else if(val==!op)
return false; for(int i=;i<=;i++){
for(int j=;j<=;j++){
if(mp[i][j]=='.'){
mp[i][j]=ch;
if(dfs(i,j,!op,step+)==false){
mp[i][j]='.';
// printf("****step=%d****%d,%d ch=%c\n",step,i,j,(op==0)?'x':'o');
if(step==){
ansx=i;ansy=j;
}
return true;
}
mp[i][j]='.';
}
}
} return false;
} int main(){
char ch[];
while(scanf("%s",ch),ch[]!='$'){
for(int i=;i<=;i++)
scanf("%s",mp[i]);
if(check_whole()==true)//特判一次!全图都为点时,不忽略的话注定超时!
printf("#####\n");
else if(dfs(,,,)==true)
printf("(%d,%d)\n",ansx,ansy);
else
printf("#####\n");
} return ;
}

(头文件都回家过五一喽了!QWQ)

 

【迭代博弈+搜索+剪枝】poj-1568--Find the Winning Move的更多相关文章

  1. POJ 1568 Find the Winning Move

    Find the Winning Move 链接 题意: 4*4的棋盘,给出一个初始局面,问先手有没有必胜策略? 有的话输出第一步下在哪里,如果有多个,按(0, 0), (0, 1), (0, 2), ...

  2. POJ 1568 Find the Winning Move(极大极小搜索)

    题目链接:http://poj.org/problem?id=1568 题意:给出一个4*4的棋盘,x和o两人轮流放.先放够连续四个的赢.给定一个局面,下一个轮到x放.问x是否有必胜策略?若有,输出能 ...

  3. poj 1568 Find the Winning Move 极大极小搜索

    思路:用极大极小搜索解决这样的问题很方便!! 代码如下: #include <cstdio> #include <algorithm> #define inf 10000000 ...

  4. 搜索+剪枝——POJ 1011 Sticks

    搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...

  5. UVA 529 - Addition Chains,迭代加深搜索+剪枝

    Description An addition chain for n is an integer sequence  with the following four properties: a0 = ...

  6. [Vijos1308]埃及分数(迭代加深搜索 + 剪枝)

    传送门 迭代加深搜索是必须的,先枚举加数个数 然后搜索分母 这里有一个强大的剪枝,就是确定分母的范围 #include <cstdio> #include <cstring> ...

  7. 搜索 + 剪枝 --- POJ 1101 : Sticks

    Sticks Problem's Link:   http://poj.org/problem?id=1011 Mean: http://poj.org/problem?id=1011&lan ...

  8. 搜索+剪枝 POJ 1416 Shredding Company

    POJ 1416 Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5231   Accep ...

  9. 迭代加深搜索 POJ 1129 Channel Allocation

    POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

随机推荐

  1. Python爬取网址中多个页面的信息

    通过上一篇博客了解到爬取数据的操作,但对于存在多个页面的网址来说,使用上一篇博客中的代码爬取下来的资料并不完整.接下来就是讲解该如何爬取之后的页面信息. 一.审查元素 鼠标移至页码处右键,选择检查元素 ...

  2. redis 部署

    wget http://download.redis.io/releases/redis-4.0.14.tar.gz tar -zxvf redis-4.0.14.tar.gz mv redis-4. ...

  3. C++标准模板库Stand Template Library(STL)简介与STL string类

    参考<21天学通C++>第15和16章节,在对宏和模板学习之后,开启对C++实现的标准模板类STL进行简介,同时介绍简单的string类.虽然前面对于vector.deque.list等进 ...

  4. idea查看接口及类的关系继承(UML)图

    选中接口或类 显示结果: 如果需要添加其他的接口或类:点击右键 添加需要的接口或类: 显示结果:

  5. Mstar方案软件运行基本原理

    1. MApp_Main.c里有个while(1)循环: 2. 通过 while(1)循环MApp_MultiTasks 里面的 MApp_ProcessUserInput 可以 得到 当前的 u8K ...

  6. LeetCode 142. 环形链表 II(Linked List Cycle II)

    142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...

  7. JUC之原子类

    在分析原子类之前,先来了解CAS操作 CAS CAS,compare and swap的缩写,中文翻译成比较并交换. CAS 操作包含三个操作数 —— 内存位置(V).预期原值(A)和新值(B).如果 ...

  8. sublime3 安装markdown插件

    sublime3 安装markdown插件 第一步安装package control 自动安装package control 手动安装package control 安装具体的markdown相关的p ...

  9. Python开发【第二章】:模块和运算符

    一.模块初识: Python有大量的模块,从而使得开发Python程序非常简洁.类库有包括三中: Python内部提供的模块 业内开源的模块 程序员自己开发的模块 1.Python内部提供一个 sys ...

  10. Consul微服务的配置中心体验篇

    Spring Cloud Consul 项目是针对Consul的服务治理实现.Consul是一个分布式高可用的系统,具有分布式.高可用.高扩展性 Consul Consul 是 HashiCorp 公 ...