Find the Winning Move
Time Limit: 3000MS   Memory Limit: 32768K
Total Submissions: 1286   Accepted: 626

Description

4x4 tic-tac-toe is played on a board with four rows (numbered 0 to 3 from top to bottom) and four columns (numbered 0 to 3 from left to right). There are two players, x and o, who move alternately with x always going first. The game is won by the first player to get four of his or her pieces on the same row, column, or diagonal. If the board is full and neither player has won then the game is a draw. 
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

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)

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剪枝]的更多相关文章

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

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

  2. 算法笔记--极大极小搜索及alpha-beta剪枝

    参考1:https://www.zhihu.com/question/27221568 参考2:https://blog.csdn.net/hzk_cpp/article/details/792757 ...

  3. 新手立体四子棋AI教程(3)——极值搜索与Alpha-Beta剪枝

    上一篇我们讲了评估函数,这一篇我们来讲讲立体四子棋的搜索函数. 一.极值搜索 极值搜索是game playing领域里非常经典的算法,它使用深度优先搜索(因为限制最大层数,所以也可以称为迭代加深搜索) ...

  4. 【poj1568】 Find the Winning Move

    http://poj.org/problem?id=1568 (题目链接) 题意 两人下4*4的井字棋,给出一个残局,问是否有先手必胜策略. Solution 极大极小搜索.. 这里有个强力优化,若已 ...

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

    poj  1568:Find the Winning Move   [迭代博弈+搜索+剪枝] 题面省略... Input The input contains one or more test cas ...

  6. POJ 1568 Find the Winning Move

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

  7. 极大极小搜索思想+(α/β)减枝 【转自-----https://blog.csdn.net/hzk_cpp/article/details/79275772】

    极大极小搜索,即minimax搜索算法,专门用来做博弈论的问题的暴力. 多被称为对抗搜索算法. 这个搜索算法的基本思想就是分两层,一层是先手,记为a,还有一层是后手,记为b. 这个搜索是认为这a与b的 ...

  8. POJ 1568 极大极小搜索 + alpha-beta剪枝

    极小极大搜索 的个人理解(alpha-beta剪枝) 主要算法依据就是根据极大极小搜索实现的. 苦逼的是,查了两个晚上的错,原来最终是判断函数写错了..瞬间吐血! ps. 据说加一句 if sum & ...

  9. [CodeVs3196]黄金宝藏(DP/极大极小搜索)

    题目大意:给出n(≤500)个数,两个人轮流取数,每次可以从数列左边或者右边取一个数,直到所有的数被取完,两个人都以最优策略取数,求最后两人所得分数. 显然这种类型的博弈题,第一眼就是极大极小搜索+记 ...

随机推荐

  1. Java NIO使用及原理分析(三)(转)

    在上一篇文章中介绍了缓冲区内部对于状态变化的跟踪机制,而对于NIO中缓冲区来说,还有很多的内容值的学习,如缓冲区的分片与数据共享,只读缓冲区等.在本文中我们来看一下缓冲区一些更细节的内容. 缓冲区的分 ...

  2. PHPstorm8 自动换行设置方法

    PHPstorm是一款非常不错的PHP开发工具,有很多需要自己设置.比如,IDE常见的代码自动换行功能需要我们自己去配置才能实现. File -> Settings ->  Editor ...

  3. MATLAB中常用的排列、组合、阶乘函数

    1.求n的阶乘,方法如下:a.factorial(n)b.gamma(n+1)c.v='n!'; vpa(v) 2.求组合(数),方法如下:a.combntns(x,m)    列举出从n个元素中取出 ...

  4. 聊聊Javascript中的AOP编程

    Duck punch 我们先不谈AOP编程,先从duck punch编程谈起. 如果你去wikipedia中查找duck punch,你查阅到的应该是monkey patch这个词条.根据解释,Mon ...

  5. [转]MySQL远程连接ERROR 2003 (HY000):Can't connect to MySQL server on'XXXXX'(111) 的问题

    问题描述: 从一台Linux远程连接另一台linux上的MySQL, 出现ERROR 2003 (HY000): Can't connect to MySQL server on 'xxx.xxx.x ...

  6. 剑指offer_面试题5_从尾到头打印链表(栈和递归实现)

    题目:输入一个链表的头结点,从尾到头反过来打印出每一个节点的值 考察 单链表操作.栈.递归等概念. 理解:要实现单链表的输出,那么就须要遍历.遍历的顺序是从头到尾.而节点输出的顺序是从尾到头.因此,先 ...

  7. perl 实现ascall 码转换

    今天需要在perl中实现一个字母表, 总不能把26个字母一个一个写出来,于是查资料,可以利用ascii码转换把数字转换成对应的字母 chr函数可以利用ascii编码把数字转换成对应的字母 perl - ...

  8. 快速找出System.Management.Automation.dll,c#调用powershell

    public static void InvokeSystemPS(string cmd) { List<string> ps = new List<string>(); ps ...

  9. 接口日志记录AOP实现-LogAspect

    使用spring aop日志记录 所需jar包 pom.xml <!-- logger begin --> <dependency> <groupId>org.sl ...

  10. sqlldr导入数据(以PostgreSql>>>Oracle为例)

    1.在目标数据库中创建表 1.1点击源表,复制创建语句 1.2 修改数据类型以匹配目标数据库,如: 字符串类型:character varying(20)>>>varchar2(20 ...