codevs 1004 四子连棋
1004 四子连棋
在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑白双方交替走棋,任意一方可以先走,如果某个时刻使得任意一种颜色的棋子形成四个一线(包括斜线),这样的状态为目标棋局。
| ● | ○ | ● | |
| ○ | ● | ○ | ● |
| ● | ○ | ● | ○ |
| ○ | ● | ○ |
从文件中读入一个4*4的初始棋局,黑棋子用B表示,白棋子用W表示,空格地带用O表示。
用最少的步数移动到目标棋局的步数。
BWBO
WBWB
BWBW
WBWO
5
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 四子连棋的更多相关文章
- codevs 1004 四子连棋 BFS、hash判重
004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋 ...
- CODEVS 1004四子连棋
[题目描述 Description] 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步,黑 ...
- Codevs p1004 四子连棋
四子连棋 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向 ...
- CODEVS——T 1004 四子连棋
http://codevs.cn/problem/1004/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Descr ...
- BFS搜索算法应用_Codevs 1004 四子连棋
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <algorithm> #include <cs ...
- 【wikioi】1004 四子连棋
题目链接 算法:BFS //2014-02-05更新 *******************************2013-10-15******************************* ...
- 迭代加深搜索[codevs1004 四子连棋]
迭代加深搜索 一.算法简介 迭代加深搜索是在速度上接近广度优先搜索,空间上和深度优先搜索相当的搜索方式.由于在使用过程中引入了深度优先搜索,所以也可以当作深度优先搜索的优化方案. 迭代加深搜索适用于当 ...
- 【宽度优先搜索】神奇的状态压缩 CodeVs1004四子连棋
一.写在前面 其实这是一道大水题,而且还出在了数据最水的OJ上,所以实际上这题并没有什么难度.博主写这篇blog主要是想写下一个想法--状态压缩.状态压缩在记录.修改状态以及判重去重等方面有着极高的( ...
- codevs1004四子连棋[BFS 哈希]
1004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗 ...
随机推荐
- spring-boot-全局异常
Spring Boot默认的异常处理机制 默认情况下,Spring Boot为两种情况提供了不同的响应方式. 一种是浏览器客户端请求一个不存在的页面或服务端处理发生异常时,一般情况下浏览器默认发送的请 ...
- plsql链接数据库配置
一. 目录结构 D:\install\PLSQL |-- instantclient_11_2 |-- tnsnames.ora |-- PLSQL ...
- 打包egg
scrapyd-deploy -p chahao -v 1.0 --build-egg chahao.egg
- idea中使用tomcat 方式启动spring boot项目
Spring boot 的main 入口启动方式相信都会用,直接运行main直接就启动了,但是往往这种方式并不是最佳的启动方式,比如运维的层面更希望调整tomcat的调优参数,而只使用嵌入启动方式很难 ...
- 工作常用shell集合
<1>日志回滚案例======>[root@localhost test]# cat hbase.sh hbase_rotate_log () { log=$1; ...
- sed实例收集
url:http://blog.csdn.net/hepeng597/article/details/7852468 一.元字符集 1)^锚定行的开始 如:/^sed/匹配所有以sed开头的行. ...
- jQuery UI 给button添加ID
$("#addOrEditApp").dialog({ modal: true ,maxHeight:dialogHeight,width:dialog_width,title: ...
- HTTP Headers解析
什么是HTTP Headers? 它包含了哪些内容? 利用requests.get()函数对豆瓣读书进行请求, 返回的r.headers如下所示: >>> import reques ...
- UFLDL 教程学习笔记(二)
课程链接:http://ufldl.stanford.edu/tutorial/supervised/LogisticRegression/ 这一节主要讲的是梯度的概念,在实验部分,比较之前的线性回归 ...
- (二)Jsoup 查找 DOM 元素
第一节: Jsoup 查找 DOM 元素 getElementById(String id) 根据 id 来查询 DOM getElementsByTag(String tagName) 根据 tag ...