迷宫问题
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status
### Description
```
定义一个二维数组:
int maze[5][5] = {

0, 1, 0, 0, 0,

0, 1, 0, 1, 0,

0, 0, 0, 0, 0,

0, 1, 1, 1, 0,

0, 0, 0, 1, 0,

};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。



### Input

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

### Output

左上角到右下角的最短路径,格式如样例所示。

### Sample Input

0 1 0 0 0

0 1 0 1 0

0 0 0 0 0

0 1 1 1 0

0 0 0 1 0

### Sample Output

(0, 0)

(1, 0)

(2, 0)

(2, 1)

(2, 2)

(2, 3)

(2, 4)

(3, 4)

(4, 4)

最简单的求最短路径问题,用BFS来解决,这里需要注意的是结构体里要定义一个字符串变量用来保存走过的路。

```c++
#include <queue>
#include <string.h>
#include <iostream>
using namespace std; #define MAXN 10
//定义方向,顶部开始,顺时针
int dir[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; int a[MAXN][MAXN];
//作用:标记,走过的结点标记为1
int vis[MAXN][MAXN]; struct Node
{
int x;
int y;
string s; //字符串s保存走过的结点
}now, nextStep; //判断结点能否通过
bool isPracticable(Node node)
{
//当超出边界,或者已经被标记访问过,或者遇到障碍物都不能再通过
if (node.x < 0 || node.x > 5 || node.y < 0 || node.y > 5 || vis[node.x][node.y] || a[node.x][node.y]) {
return 0;
}
return 1;
} void BFS()
{
queue<Node> Q;
now.x = 0;
now.y = 0;
now.s = "00";
Q.push(now);
vis[now.x][now.y] = 1; while (!Q.empty()) {
//获取队列首部元素
now = Q.front();
if (now.x == 4 && now.y == 4) { //走到右下角,输出结果
for (int i = 0; i < now.s.length(); i = i + 2) {
cout << "(" << now.s[i] << ", " << now.s[i+1] << ")" << endl;
}
return;
} for (int i = 0; i < 4; i++) { //按照上、右、下、左的方向搜索,搜索方向可随意,但是要保证四个方向都被搜索一遍
nextStep.x = now.x + dir[i][0];
nextStep.y = now.y + dir[i][1];
nextStep.s = now.s;
if (isPracticable(nextStep)) {
char x = nextStep.x + 48;
char y = nextStep.y + 48;
nextStep.s += x;
nextStep.s += y;
Q.push(nextStep);
vis[nextStep.x][nextStep.y] = 1;
}
}
//把对首元素排出队列
Q.pop();
}
} int main(int argc, const char * argv[]) { for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cin >> a[i][j];
}
}
BFS();
return 0;
}

迷宫-BFS的更多相关文章

  1. ZOJ 1649 Rescue(有敌人迷宫BFS)

    题意 求迷宫中从a的位置到r的位置须要的最少时间  经过'.'方格须要1s  经过'x'方格须要两秒  '#'表示墙 因为有1s和2s两种情况  须要在基础迷宫bfs上加些推断 令到达每一个点的时间初 ...

  2. POJ 2251 Dungeon Master(3D迷宫 bfs)

    传送门 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 11 ...

  3. poj 1383 Labyrinth【迷宫bfs+树的直径】

    Labyrinth Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 4004   Accepted: 1504 Descrip ...

  4. hdu_1728_逃离迷宫(bfs)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1728 题意:走迷宫,找最小的拐角 题解:对BFS有了新的理解,DFS+剪枝应该也能过,用BFS就要以拐 ...

  5. hdu 1728 逃离迷宫 (BFS)

    逃离迷宫 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

  6. HDU1728-逃离迷宫-BFS

    逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. HDU 1026(迷宫 BFS+打印)

    题意是要穿过一个迷宫并且将每一步打印出来. 用宽搜的方法找到路径,在 vis 中存一下方向,只是这题被看到的一种不太对的运算符重载坑了很久...... 代码如下: #include <bits/ ...

  8. Applese走迷宫-bfs

    链接:https://ac.nowcoder.com/acm/contest/330/C来源:牛客网 题目描述 精通程序设计的 Applese 双写了一个游戏. 在这个游戏中,它被困在了一个 n×mn ...

  9. hdu 1728 逃离迷宫 bfs记转向

    题链:http://acm.hdu.edu.cn/showproblem.php?pid=1728 逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Mem ...

随机推荐

  1. Python调用Linux bash命令

    import subprocess as sup  # 以下注释很多(为了自己以后不忘), 如果只是想在python中执行Linux命令, 看前5行就够了 # 3.5版本之后官方推荐使用sup.run ...

  2. SQL Server数据库的兼容级别

    SQL Server 是Microsoft 公司推出的关系型数据库管理系统.具有使用方便可伸缩性好与相关软件集成程度高等优点,可跨越从运行Microsoft Windows 98 的膝上型电脑到运行M ...

  3. Mysql必知必会 第一章 了解SQL

    第一章 了解SQL 1.1 数据库基础 1.1.1 什么是数据库 数据库的定义:保存有组织的数据的容器 数据库软件不是数据库,而是DBMS 1.1.2 表 表(Table)的定义:某种特定类型数据的结 ...

  4. 00.pt-toolkit 目录

    一. 好用便利的工具,常用 pt-align 对齐文本格式pt-archiver 循序渐进的归档表,删除表,迁移数据pt-config-diff 对比不同配置文件.服务器配置参数pt-diskstat ...

  5. getQueryString.js

    function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&] ...

  6. python的文件读写笔记

    读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘, ...

  7. 《Linux就该这么学》第十二天课程

    使用ssh服务管理远程主机 绑定两块网卡 原创地址:https://www.linuxprobe.com/chapter-09.html 第1步:在虚拟机系统中再添加一块网卡设备,请确保两块网卡都处在 ...

  8. flink 读取kafka 数据,partition分配

    每个并发有个编号,只会读取kafka partition  % 总并发数 == 编号 的分区   如: 6 分区, 4个并发 分区: p0 p1 p2 p3 p4 p5 并发: 0 1 2 3    ...

  9. HDU 6397 Character Encoding (组合数学 + 容斥)

    题意: 析:首先很容易可以看出来使用FFT是能够做的,但是时间上一定会TLE的,可以使用公式化简,最后能够化简到最简单的模式. 其实考虑使用组合数学,如果这个 xi 没有限制,那么就是求 x1 + x ...

  10. Codeforces Round #485 (Div. 2) E. Petr and Permutations

    Codeforces Round #485 (Div. 2) E. Petr and Permutations 题目连接: http://codeforces.com/contest/987/prob ...