题目描述 Description

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

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

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

样例输入 Sample Input

BWBO
WBWB
BWBW
WBWO

样例输出 Sample Output

5

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string> using namespace std;
const int maxn = ; struct member{
int last,step;
int board[][];//[y][x]
};
member first,q[maxn];
int tot;
int dx[] = {,-,,};
int dy[] = {-,,,}; bool judge(member temp){
int r1 = ,r2 = ;
for(r1 = ;r1 <= ;r1++){
bool sign = true;
for(r2 = ;r2 <= ;r2++){
if(temp.board[r1][r2] != temp.board[r1][r2+]) sign = false;
}
if(sign) return true;
}
for(r1 = ;r1 <= ;r1++){
bool sign = true;
for(r2 = ;r2 <= ;r2++){
if(temp.board[r2][r1] != temp.board[r2+][r1]) sign = false;
}
if(sign) return true;
}
if(temp.board[][] == temp.board[][] && temp.board[][] == temp.board[][] && temp.board[][] == temp.board[][]) return true;
if(temp.board[][] == temp.board[][] && temp.board[][] == temp.board[][] && temp.board[][] == temp.board[][]) return true;
return false;
} int bfs(){
int h = ,t = ,nx,ny;
member tailer;
while(h <= t){
if(judge(q[h])) return q[h].step;
int f1 = ,f2 = ,ya,xa,yb,xb,sign = ;
for(f1 = ;f1 <= ;f1++){
for(f2 = ;f2 <= ;f2++){
if(q[h].board[f1][f2] == ){
if(sign == ){
sign = ;
ya = f1;
xa = f2;
}else if(sign == ){
yb = f1;
xb = f2;
}
}
}
}
f1 = f2 = ;
for(f1 = ;f1 < ;f1++){
nx = xa + dx[f1];
ny = ya + dy[f1];
if(nx <= && nx >= && ny <= && ny >= && q[h].board[ny][nx] != q[h].last){
t++;
tailer = q[h];
tailer.last = tailer.board[ny][nx];
swap(tailer.board[ny][nx],tailer.board[ny - dy[f1]][nx - dx[f1]]);
tailer.step++;
q[t] = tailer; }
nx = xb + dx[f1];
ny = yb + dy[f1];
if(nx <= && nx >= && ny <= && ny >= && q[h].board[ny][nx] != q[h].last){
t++;
tailer = q[h];
tailer.last = tailer.board[ny][nx];
swap(tailer.board[ny][nx],tailer.board[ny - dy[f1]][nx - dx[f1]]);
tailer.step++;
q[t] = tailer; } }
h++;
} } int main(){
char cmd;
int r1,r2,ans;
for(r1 = ;r1 <= ;r1++){
for(r2 = ;r2 <= ;r2++){
cin>>cmd;
if(cmd == 'O') first.board[r1][r2] = ;
if(cmd == 'B') first.board[r1][r2] = ;
if(cmd == 'W') first.board[r1][r2] = ;
}
}
first.step = ;
first.last = ;
q[] = first;
ans = bfs();
cout<<ans;
return ;
}

codevs1004 四子连棋的更多相关文章

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

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

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

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

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

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

  4. codevs1004四子连棋

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

  5. Codevs p1004 四子连棋

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

  6. codevs 1004 四子连棋

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

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

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

  8. 【洛谷 P2346】四子连棋(状态压缩,搜索)

    其实这题可以直接二进制状压做,1表示黑棋,0表示白棋,另外记录下2个空点的位置就行了. 具体看代码(冗长): #include <iostream> #include <cstdio ...

  9. P2346 四子连棋

    P2346 四子连棋 迭代加深++ 题意描述 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋 ...

随机推荐

  1. 转 phpmyadmin操作技巧:如何在phpmyadmin里面复制mysql数据库?

    对于每一个站长而言,都会遇到要进行网站测试的时候.这个时候,往往需要备份数据库.如果按照一般的操作方式,都是先把数据库导出并备份到本地,然后再服务器上测试.如果一切正常还好,一旦出了问题,就又得把数据 ...

  2. [转]MVC4项目中验证用户登录一个特性就搞定

    本文转自:http://www.mrhuo.com/Article/Details/470/A-Attribute-For-MVC4-Project-Used-To-Validate-User-Log ...

  3. C#基础 进制转换6/17

    二进制→十进制: 计算公式:a*20+b*21+c*22+…+m*2(n-1) 公式中a为二进制数右边第一位数,b为第二位数,以此类推 例:二进制1011010转换为十进制数为 0*20+1*21+0 ...

  4. Sql 存储过程动态添加where条件

    )= '2,3' )= '' ) if(@bussHallId is not null) set @strWhere = @strWhere + ' and bh.ID in ('+@bussHall ...

  5. [ Luogu 3935 ] Calculating

    \(\\\) \(Description\) 若\(x\)分解质因数结果为\(x=p_1^{k_1}p_2^{k_2}\cdots p_n^{k_n}\),令\(f(x)=(k_1+1)(k_2+1) ...

  6. java继承问题

    代码: 父类: public class Father { public Father() { System.out.println("基类构造函数{"); show(); new ...

  7. MySQL(四)DQL语言——条件查询

    摘要:条件查询:条件表达式,逻辑表达式,模糊查询,like,通配符,转义字符,escape关键字,between and,in,is null,is not null,安全等于. 条件查询语法: SE ...

  8. JavaScipt30(第五个案例)(主要知识点:flex布局)

    承接上文,这是第5个案例:这节没什么讲的,随便记录下吧,主要是用了flex布局与transform translateY,js部分和案例1类似. 附上项目链接: https://github.com/ ...

  9. 00Cascading Style Sheet

    Cascading Style Sheet CSS(Cascading Style Sheet)即层叠样式表,简称样式表.要理解层叠样式表的概念先要理解样式的概念.样式就是对网页中的 元素(字体.段落 ...

  10. 06Microsoft SQL Server 完整性约束

    Microsoft SQL Server 完整性约束 标识 IDENTITY自动编号 CREATE TABLE table_name( id ,), NAME ) not null, sex ) de ...