题目描述 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. java HashMap和LinkedHashMap区别

    我们用的最多的是HashMap,在Map 中插入.删除和定位元素,HashMap 是最好的选择.但如果您要按自然顺序或自定义顺序遍历键,那么TreeMap会更好.如果需要输出的顺序和输入的相同,那么用 ...

  2. [读书笔记3]《C语言嵌入式系统编程修炼》

    第五章 性能优化   5.1 使用宏定义 在C语言中,宏是产生内嵌代码的唯一方法.对于嵌入式系统而言,为了能达到性能要求,宏是一种很好的代替函数的方法.   写一个"标准"宏MIN ...

  3. 三分 POJ 2420 A Star not a Tree?

    题目传送门 /* 题意:求费马点 三分:对x轴和y轴求极值,使到每个点的距离和最小 */ #include <cstdio> #include <algorithm> #inc ...

  4. C#知识点-枚举器和迭代器

    一.几个基本概念的理解 问题一:为什么数组可以使用foreach输出各元素 答:数组是可枚举类型,它实现了一个枚举器(enumerator)对象:枚举器知道各元素的次序并跟踪它们的位置,然后返回请求的 ...

  5. 实现div左右上下都居中

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. overflow实现隐藏滚动条同时又可以滚动

    .scroll-list ul{ white-space: nowrap; -webkit-overflow-scrolling: touch; overflow-x: auto; overflow- ...

  7. Javascript DOM 编程艺术(第二版)读书笔记——DOM基础

    1.DOM是什么 D=document(文档) O=object(对象) M=Model(模型) DOM又称节点树 一些术语: parent(父)   child(子)   sibling(兄弟)   ...

  8. Android常用依赖库搜集

    图片处理 CircleImageView Git地址:https://github.com/hdodenhof/CircleImageView 图片依赖库 glide Git地址:https://gi ...

  9. canvas一周一练 -- canvas绘制中国银行标志(4)

    运行效果: <!DOCTYPE html> <html> <head> </head> <body> <canvas id=" ...

  10. blender--(凹凸贴图)................https://jingyan.baidu.com/article/9f63fb917c4becc8400f0ea8.html

    在blender中直接绘制模型凹凸纹理细节 听语音 | 浏览:32 | 更新:2018-02-20 11:18 1 2 3 4 5 6 7 分步阅读 在blender中为了表现更多的模型细节,我们会常 ...