https://www.luogu.org/problemnew/show/P2346

广搜

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue> #define maxn 10010
#define inf 1000000007
#define ll long long using namespace std;
int n, m, ans = inf; int dx[] = {, , , -, };
int dy[] = {, , , , -}; struct Node {
int a[][];
int color;
int step;
} t; queue <Node> q; bool check(Node f) {
for(int i = ; i <= ; i ++) {
if(f.a[i][] == f.a[i][] && f.a[i][] == f.a[i][] && f.a[i][] == f.a[i][]) return ;
if(f.a[][i] == f.a[][i] && f.a[][i] == f.a[][i] && f.a[][i] == f.a[][i]) return ;
}
if(f.a[][] == f.a[][] && f.a[][] == f.a[][] && f.a[][] == f.a[][]) return ;
if(f.a[][] == f.a[][] && f.a[][] == f.a[][] && f.a[][] == f.a[][]) return ;
return ;
} void init() { //处理一开始的棋子颜色
t.step = ;
t.color = ;
for(int i = ; i <= ; i ++)
for(int j = ; j <= ; j ++)
if(t.a[i][j] == )
for(int k = ; k <= ; k ++) {
int x = i + dx[k];
int y = j + dy[k];
if(t.a[x][y] == ) continue ;
if(x >= && x <= && y >= && y <= ) { //注意边界
Node c = t;
c.color = t.a[x][y];
c.step = ;
swap(c.a[i][j], c.a[x][y]);//移动棋子
q.push(c);
}
}
} void bfs() { //广搜模板
while(!q.empty()) {
Node b = q.front();
q.pop();
if(check(b)) {
ans = b.step;
return ;
}
for(int i = ; i <= ; i ++)
for(int j = ; j <= ; j ++) {
if(b.a[i][j] == ) {
for(int k = ; k <= ; k ++) {
int x = i + dx[k];
int y = j + dy[k];
if(x >= && x <= && y >= && y <= && b.a[x][y] == (b.color ^ )) { //黑白交替移动
Node c = b;
swap(c.a[i][j], c.a[x][y]);//移动棋子
c.color = b.color ^ ;//如果上一步走的是黑,这一步就是白,上一步是白,这一步是黑。
c.step = b.step + ;//步数加一
q.push(c);//将当前状态入队
}
}
}
}
}
} int main() {
int x, y, z;
char s[];
for(int i = ; i <= ; i ++) {
scanf("%s",s+);
for(int j = ; j <= ; j ++) { //处理图,便于广搜
if(s[j] == 'B') t.a[i][j] = ;
if(s[j] == 'W') t.a[i][j] = ;
if(s[j] == 'O') t.a[i][j] = ;
}
}
init();
bfs();
printf("%d\n", ans);
return ;
}

[Luogu] 四子连棋的更多相关文章

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

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

  2. Codevs p1004 四子连棋

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

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

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

  4. codevs 1004 四子连棋

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

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

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

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

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

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

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

  8. codevs1004四子连棋

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

  9. P2346 四子连棋

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

随机推荐

  1. Linux系统下如何优雅地关闭Java进程?

    资料出处: http://www.sohu.com/a/329564560_700886 https://www.cnblogs.com/nuccch/p/10903162.html 前言 Linux ...

  2. (二十四)JSP标签之基本标签(<jsp:标签名>)

    一.常用标签 1.1 jsp中标签一共有8中,其中常用的有6中,本文将介绍这6种常用的标签. 1.2 6种标签 1. <jsp:include> <jsp:include>标签 ...

  3. (九)springmvc之json的数据请求(客户端发送json数据到服务端)

    index.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pag ...

  4. 微软发布云端基因服务:推动AI驱动的精准医疗

    微软发布云端基因服务:推动AI驱动的精准医疗 2018年03月07日 00:00:00 微软研究院AI头条 阅读数:117    版权声明:本文为博主原创文章,未经博主允许不得转载. https:// ...

  5. opencv-01--图像的遍历

    遍历图像的4种方式 一.at<typename>(i,j) Mat类提供了一个at的方法用于取得图像上的点,它是一个模板函数,可以取到任何类型的图像上的点.下面我们通过一个图像处理中的实际 ...

  6. MiniUI学习笔记一【转】

    MiniUI Api文档:http://miniui.com/docs/api/index.html 1.取组件值 传递form data,load发送 请求加载数据 <script type= ...

  7. Django2.0 开始一个项目

    python项目运行环境: 安装虚拟环境工具   pip install virtualenv 使用虚拟环境: 创建虚拟环境: virtualenv  <虚拟环境名称> 进去虚拟环境: S ...

  8. Linux查看系统及版本信息

    1.查看操作系统版本cat /proc/version 2.查看系统发行版cat /etc/issue 或cat /etc/redhat-release 3.查看系统内核信息uname -a

  9. win10下PLSQL Developer 连接ubuntu上安装的oracle 11g

    说明:过程记录的不是很相信,只记录基本步骤.并不适合想一步一步照做的同学. win10下需要的操作 1.微软官网下载instantclient,然后接到到本地一个文件夹,注意路径不要又空格,中文和括号 ...

  10. js大数计算之展示

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