1004 四子连棋

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
 
题目描述 Description

在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑白双方交替走棋,任意一方可以先走,如果某个时刻使得任意一种颜色的棋子形成四个一线(包括斜线),这样的状态为目标棋局。

 
 
输入描述 Input Description
从文件中读入一个4*4的初始棋局,黑棋子用B表示,白棋子用W表示,空格地带用O表示。
输出描述 Output Description

用最少的步数移动到目标棋局的步数。

样例输入 Sample Input

BWBO
WBWB
BWBW
WBWO

样例输出 Sample Output

5

数据范围及提示 Data Size & Hint

hi

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int map[][];
int move[]={,,,-,};
int bor;
string _;int x[],y[],ans=0x7fffffff;
bool can(int x,int y,int z)
{
if(x>=&&x<=&&y>=&&y<=&&z!=map[x][y])return ;else return ;
}
bool check()
{
if(map[][]==map[][]&&map[][]==map[][]&&map[][]==map[][])return ;
if(map[][]==map[][]&&map[][]==map[][]&&map[][]==map[][])return ;
if(map[][]==map[][]&&map[][]==map[][]&&map[][]==map[][])return ;
if(map[][]==map[][]&&map[][]==map[][]&&map[][]==map[][])return ;
if(map[][]==map[][]&&map[][]==map[][]&&map[][]==map[][])return ;
if(map[][]==map[][]&&map[][]==map[][]&&map[][]==map[][])return ;
else return ;
}
bool dfs(int x1,int y1,int who,int x2,int y2,int step)
{
if(step==bor)
{
if(check())return ;
else return ;
}
int next_x1,next_x2,next_y1,next_y2;
for(int i=;i<;i++)
{
next_x1=x1+move[i];
next_y1=y1+move[i+];
next_x2=x2+move[i];
next_y2=y2+move[i+];
if(can(next_x1,next_y1,who))
{
int sssy1;
if(who==)
sssy1=;else sssy1=;
swap(map[x1][y1],map[next_x1][next_y1]);
if(dfs(next_x1,next_y1,sssy1,x2,y2,step+))return ;
swap(map[x1][y1],map[next_x1][next_y1]);
}
if(can(next_x2,next_y2,who))
{
int sssy2;
if(who==)sssy2=;else sssy2=;
swap(map[x2][y2],map[next_x2][next_y2]);
if(dfs(x1,y1,sssy2,next_x2,next_y2,step+))return ;
swap(map[x2][y2],map[next_x2][next_y2]);
}
}
return ;
}
int main()
{
int pppppp=;
for(int i=;i<=;i++)
{
cin>>_;
for(int j=;j<;j++)
{
if(_[j]=='B')map[i][j+]=;
else if(_[j]=='W')map[i][j+]=;
else
{
x[pppppp]=i;
y[pppppp++]=j+;
}
}
}
for(bor=;;bor++)
{
if(dfs(x[],y[],,x[],y[],))break;
if(dfs(x[],y[],,x[],y[],))break;
}
printf("%d",bor);
}

codevs 1004 四子连棋的更多相关文章

  1. codevs 1004 四子连棋 BFS、hash判重

    004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold       题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋 ...

  2. CODEVS 1004四子连棋

    [题目描述 Description] 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑 ...

  3. Codevs p1004 四子连棋

                          四子连棋 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向 ...

  4. CODEVS——T 1004 四子连棋

    http://codevs.cn/problem/1004/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Descr ...

  5. BFS搜索算法应用_Codevs 1004 四子连棋

    #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <algorithm> #include <cs ...

  6. 【wikioi】1004 四子连棋

    题目链接 算法:BFS //2014-02-05更新 *******************************2013-10-15******************************* ...

  7. 迭代加深搜索[codevs1004 四子连棋]

    迭代加深搜索 一.算法简介 迭代加深搜索是在速度上接近广度优先搜索,空间上和深度优先搜索相当的搜索方式.由于在使用过程中引入了深度优先搜索,所以也可以当作深度优先搜索的优化方案. 迭代加深搜索适用于当 ...

  8. 【宽度优先搜索】神奇的状态压缩 CodeVs1004四子连棋

    一.写在前面 其实这是一道大水题,而且还出在了数据最水的OJ上,所以实际上这题并没有什么难度.博主写这篇blog主要是想写下一个想法--状态压缩.状态压缩在记录.修改状态以及判重去重等方面有着极高的( ...

  9. codevs1004四子连棋[BFS 哈希]

    1004 四子连棋   时间限制: 1 s   空间限制: 128000 KB   题目等级 : 黄金 Gold   题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗 ...

随机推荐

  1. hdu 5438 Ponds(长春网络赛 拓扑+bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5438 Ponds Time Limit: 1500/1000 MS (Java/Others)     ...

  2. perl6 Net::HTTP 不能发送https请求

    如下命安装必要的包: sudo apt install libssl1.0.0 libssl-dev zef install IO::Socket::SSL zef install Net::HTTP

  3. ubuntu git 简单入门【转】

    转自:http://blog.chinaunix.net/uid-20718384-id-3334859.html 1. 安装 sudo apt-get install git-core 2.  初始 ...

  4. numpy细碎知识点

    np.random.rand() 基于python自带模块random的random函数的一个延伸吧,生成指定数量的列表 np.random.rand(a,b) 参数a,b均为整型,生成含有a个元素的 ...

  5. Python和MySQL数据库交互PyMySQL

    Python数据库操作 对于关系型数据库的访问,Python社区已经指定了一个标准,称为Python Database API SepcificationV2.0.MySQL.Qracle等特定数据库 ...

  6. 经典面试题:n个数字(0,1,…,n-1)形成一个圆圈

    题目: n个数字(0,1,…,n-1)形成一个圆圈,从数字0开始, 每次从这个圆圈中删除第m个数字(第一个为当前数字本身,第二个为当前数字的下一个数字). 当一个数字删除后,从被删除数字的下一个继续删 ...

  7. Struts – MappingDispatchAction Example

    Struts MappingDispatchAction class is used to group similar functionality into a single action class ...

  8. 实战MEF(1)一种不错的扩展方式

    在过去,我们完成一套应用程序后,如果后面对其功能进行了扩展或修整,往往需要重新编译代码生成新的应用程序,然后再覆盖原来的程序.这样的扩展方式对于较小的或者不经常扩展和更新的应用程序来说是可以接受的,而 ...

  9. 防止一个exe被打开多次

    mutex有一个名字,如果这个exe已经打开了,createNew返回的就是false,程序就退出了. 这是个wpf application的例子 protected override void On ...

  10. hdu 2069 1 5 10 25 50 这几种硬币 一共100个(母函数)

    题意: 有50 25 10 5 1 的硬币 一共最多有100枚 输入n输出有多少种表示方法 Sample Input1126 Sample Output413 # include <iostre ...