题目描述 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. Linux day01(一) 创建Linux虚拟机,设置虚拟机默认属性,虚拟机和Xhell建立连接

    一:创建Linux虚拟机步骤: 1. 二:设置虚拟机默认属性 三:虚拟机和Xhell建立连接

  2. Spring.Net学习笔记(二)-数据访问器

    Spring对ADO.NET也提供了支持,依赖与程序集Spring.Data.dll IDbProvider IDbProvider定义了数据访问提供器的基础,配置如下 <?xml versio ...

  3. CF897C Nephren gives a riddle

    思路: 递归. 比赛的时候脑抽了len[]没算够,wa了几次. 实现: #include <bits/stdc++.h> using namespace std; using ll = l ...

  4. from scipy import spatial 出现 from .qhull import * ImportError: DLL load failed: The specified module could not be found. 错误

    错误描述: 本人机器window8.1 64位,python2.7. Traceback (most recent call last): File "C:/Users/Hamid/Docu ...

  5. Unity笔记(3)自学第二天

    学习记录: 界面使用: 脚本使用: 脚本注意点:

  6. nw.js开发第一个程序(html开发桌面程序exe)

    一.环境配置 windows系统 cnpm install node 下载nw.js https://github.com/nwjs/nw.js 找到download下载合适的版本 二.开发 项目目录 ...

  7. js异步请求

    目前async / await特性并没有被添加到ES2016标准中,但不代表这些特性将来不会被加入到Javascript中.在我写这篇文章时,它已经到达第三版草案,并且正迅速的发展中.这些特性已经被I ...

  8. 微服务网关从零搭建——(八)Ocelot网关中加入skywalking APM

    准备工作 一.下载skywalking 本例使用的是 注: 1.解压后执行完2,3步骤后运行\bin\startup.bat 2.默认后台端口为8080 如需修改则修改\webapp\webapp.y ...

  9. datatable 分组

    public static void PrintPersons() { //准备数据 DataTable dt = new DataTable(); dt.Columns.Add(new DataCo ...

  10. tab下拉显示

    <!DOCTYPE html> <html lang="en"> <head>     <meta charset="UTF-8 ...