#include <iostream>
#include <queue> using namespace std; /*
5 4
0 0 1 0
0 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
0 0 4 1 6
--------------------------------
Process exited with return value 0
Press any key to continue . . .
*/ //一个坐标节点
class Node
{
public:
Node(int _x, int _y, int _flag, int _step)
{
this->x = _x;
this->y = _y;
this->flag = _flag;
this->step = _step;
}
public:
int x;
int y;
int flag;
int step;
}; int n, m;// 迷宫行、列
int beginx, beginy, endx, endy;// 入口坐标,出口坐标
int maze[20][20] = {0}, book[20][20] = {0};// 存放迷宫、标记是否访问过
int direction[4][2] = { {0, 1}, {1, 0}, {0, -1}, {-1, 0} };// 方向数组
queue<Node> qmaze;// 队列 void BFS()
{
//创建一个坐标节点
Node firtnode(beginx, beginy, 0, 0); //压入队列,并设置为已经访问
qmaze.push(firtnode);
book[beginx][beginy] = 1; //队列不为空时
while(!qmaze.empty())
{
//是否找到出口
int flag = 0; //枚举四个方向
for(int i = 0; i <= 3; i++)
{
//获取下一个节点的坐标
int tx = qmaze.front().x + direction[i][0];
int ty = qmaze.front().y + direction[i][1]; //是否越界
if(tx < 0 || tx > n - 1 || ty < 0 || ty > m - 1)
{
continue;
} //是否已经访问过,是否是路
if(maze[tx][ty] == 0 && book[tx][ty] == 0)
{
book[tx][ty] = 1; Node & tempnode = qmaze.front();
Node node(tx, ty, tempnode.flag, tempnode.step += 1); qmaze.push(node);
} //如果找到,则跳出
if(tx == endx && ty == endy)
{
flag = 1;
break;
}
} //如果找到,则跳出
if(flag)
{
break;
} qmaze.pop();
}
} int main()
{
cin >> n >> m;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
cin >> maze[i][j];
cin >> beginx >> beginy >> endx >> endy; BFS(); Node & result = qmaze.front();
cout << endl << result.step; return 0;
}

  

BFS-广度优先遍历的更多相关文章

  1. 广度优先遍历-BFS、深度优先遍历-DFS

    广度优先遍历-BFS 广度优先遍历类似与二叉树的层序遍历算法,它的基本思想是:首先访问起始顶点v,接着由v出发,依次访问v的各个未访问的顶点w1 w2 w3....wn,然后再依次访问w1 w2 w3 ...

  2. 算法学习 - 图的广度优先遍历(BFS) (C++)

    广度优先遍历 广度优先遍历是非经常见和普遍的一种图的遍历方法了,除了BFS还有DFS也就是深度优先遍历方法.我在我下一篇博客里面会写. 遍历过程 相信每一个看这篇博客的人,都能看懂邻接链表存储图. 不 ...

  3. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

  4. 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)

    一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...

  5. 图的深度优先遍历(DFS)和广度优先遍历(BFS)

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  6. 饥饿的小易(枚举+广度优先遍历(BFS))

    题目描述 小易总是感觉饥饿,所以作为章鱼的小易经常出去寻找贝壳吃.最开始小易在一个初始位置x_0.对于小易所处的当前位置x,他只能通过神秘的力量移动到 4 * x + 3或者8 * x + 7.因为使 ...

  7. 怎样实现广度优先遍历(BFS)

    BFS过程: 一:訪问顶点V,并标记V为已经訪问 二:顶点V入队列 三:假设队列非空.进行运行,否则算法结束 四:出队列取得对头顶点u,假设顶点未被訪问,就訪问该顶点,并标记该顶点为已经訪问 五:查找 ...

  8. 图的广度优先遍历(bfs)

    广度优先遍历: 1.将起点s 放入队列Q(访问) 2.只要Q不为空,就循环执行下列处理 (1)从Q取出顶点u 进行访问(访问结束) (2)将与u 相邻的未访问顶点v 放入Q, 同时将d[v]更新为d[ ...

  9. 图的广度优先遍历算法(BFS)

    在上一篇文章我们用java演示了图的数据结构以及图涉及到的深度优先遍历算法,本篇文章将继续演示图的广度优先遍历算法.广度优先遍历算法主要是采用了分层的思想进行数据搜索.其中也需要使用另外一种数据结构队 ...

  10. 图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS)

    参考网址:图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS) - 51CTO.COM 深度优先遍历(Depth First Search, 简称 DFS) 与广度优先遍历(Breath ...

随机推荐

  1. 请通过vim练习:vim vimtutor

    vim vimtutor ================================================================================ W e l ...

  2. MySQL 主外键约束与标准SQL不同的地方

    [标准SQL的外键约束条件] 1): 子表引用父表的主键 drop table if exists child,parent; create table if not exists parent( i ...

  3. 全相FFT

    作者:桂. 时间:2017-12-02  23:29:48 链接:http://www.cnblogs.com/xingshansi/p/7956491.html 一.相位提取 以正弦信号为例,x = ...

  4. 闭包(Closures)

    本文转自:http://goddyzhao.tumblr.com/post/11311499651/closures 翻译自:http://dmitrysoshnikov.com/ 概要 本文将介绍一 ...

  5. js json转url参数

    var json = { sh: '上海' } var params = Object.keys(json).map(function (key) { // body... return encode ...

  6. javascript原生代码取单选框的值

    网上搜索到的,项目中正好用到,先记下来,以后直接从自己的博客上复制粘贴!!! //取单选框选择中的值,传入单选框的name function getRadioBoxValue(radioName) { ...

  7. socket.io笔记二之事件监听回调函数接收一个客户端的回调函数

    //服务端 socket.on('test', function (name, fn) { console.log(name) //输出yes fn('woot'); }); //客户端 socket ...

  8. 每日英语:The Most Destructive, Unpredictable Force in Tech

    What's the most destructive force in the tech world, the thing that has nearly killed BlackBerry, pu ...

  9. pre 标签的使用注意事项

    .news-msg{ // padding: 5px; white-space: pre-wrap; word-wrap: break-word; font-family:'微软雅黑'; } < ...

  10. ostream_iterator的可能实现

    当我们要输出一个容器的内容时,可以使用std::copy函数,如下: vector <string> myvector; std::copy(myvector.begin(), myvec ...