2022.01.04
广度优先遍历(BFS)
BFS通常用队列queue来实现,其所占用的空间为O(2^h)。
当边权都是1时,最短路问题可用BFS来解决。
一个常用的队列用法:
while(队列不空){
t <- 队头;
拓展t的邻点x
if(x满足条件:未遍历、可到达、矩阵内){
queue <- x;
d[x] = d[t] + 1;
}
}
可用数组q[]来模拟队列:
hh = 0, tt = -1;
while(hh <= tt){
auto t = q[hh ++];
拓展{
...
q[++ tt] = x;
}
}
走迷宫
给定一个 n×m 的二维整数数组,用来表示一个迷宫,数组中只包含 0 或 1,其中 0 表示可以走的路,1 表示不可通过的墙壁。
最初,有一个人位于左上角 (1,1) 处,已知该人每次可以向上、下、左、右任意一个方向移动一个位置。
请问,该人从左上角移动至右下角 (n,m) 处,至少需要移动多少次。
数据保证 (1,1) 处和 (n,m) 处的数字为 0,且一定至少存在一条通路。
输入格式
第一行包含两个整数 n 和 m。
接下来 n 行,每行包含 m 个整数(0 或 1),表示完整的二维数组迷宫。
输出格式
输出一个整数,表示从左上角移动至右下角的最少移动次数。
数据范围
1≤n,m≤100
输入样例:
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
输出样例:
8
思路
bfs.
g[][]用于储存图数据,d[][]储存图上点的最短距离,初始值为-1。
对于队头,每次搜索四个方向,若在矩阵内、可走、未走过,则将其放入队尾。
代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef pair<int, int> PII;
const int N = 110;
int n, m;
PII q[N * N]; // 模拟队列
int g[N][N], d[N][N]; // 存储图信息和距离
PII pr[N][N]; // 若需要输出路径,则记录从哪个点过来
int bfs()
{
int hh = 0, tt = 0;
q[0] = {0, 0}; // 初始化队头
memset(d, -1, sizeof d);
d[0][0] = 0;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
while(hh <= tt)
{
auto t = q[hh ++];
for(int i = 0; i < 4; i ++ )
{
int x = t.first + dx[i], y = t.second + dy[i];
if(x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0 && d[x][y] == -1)
{
d[x][y] = d[t.first][t.second] + 1; // 更新距离矩阵
q[++ tt] = {x, y}; // 入队尾
pr[x][y] = t; // 存前驱结点
}
}
}
// int x = n - 1, y = m - 1;
// while(x || y){
// cout << x << " " << y << endl;
// auto t = pr[x][y];
// x = t.first, y = t.second;
// }
return d[n - 1][m - 1];
}
int main()
{
cin >> n >> m;
for(int i = 0; i < n; i ++ )
for(int j = 0; j < m; j ++ )
cin >> g[i][j];
cout << bfs() << endl;
return 0;
}
// 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
使用C++STL
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 110;
int n, m;
queue<PII> q;
int g[N][N], d[N][N]; // 存储图信息和距离
PII pr[N][N]; // 若需要输出路径,则记录从哪个点过来
int bfs()
{
int hh = 0, tt = 0;
q.push({0, 0}); // 初始化队头
memset(d, -1, sizeof d);
d[0][0] = 0;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
while(q.size())
{
auto t = q.front(); // 队头
q.pop(); // 出队
for(int i = 0; i < 4; i ++ )
{
int x = t.first + dx[i], y = t.second + dy[i];
if(x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0 && d[x][y] == -1)
{
d[x][y] = d[t.first][t.second] + 1; // 更新距离矩阵
q.push({x, y}); // 入队尾
pr[x][y] = t; // 存前驱结点
}
}
}
// int x = n - 1, y = m - 1;
// while(x || y){
// cout << x << " " << y << endl;
// auto t = pr[x][y];
// x = t.first, y = t.second;
// }
return d[n - 1][m - 1];
}
int main()
{
cin >> n >> m;
for(int i = 0; i < n; i ++ )
for(int j = 0; j < m; j ++ )
cin >> g[i][j];
cout << bfs() << endl;
return 0;
}
// 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
Python3
def bfs():
d[0][0] = 0
queue = [(0, 0)]
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
while queue : #队列不为空
x, y = queue.pop(0)
for i in range(4):
a = x + dx[i];
b = y + dy[i];
if a >= 0 and a < n and b >= 0 and b < m and g[a][b] == 0 and d[a][b] == -1:
queue.append((a,b))#入队
d[a][b] = d[x][y] + 1
print(d[n - 1][m - 1])
n, m = map(int, input().split()) # map函数对分割输入后的字符列表转换成整型
g = [[-1 for j in range(m)] for i in range(n)] # 存储地图,先初始化
for i in range(n):
input_col = list(map(int, input().split()))
for j in range(m):
g[i][j] = input_col[j];
d = [[-1 for i in range(m)] for j in range(n)]#初始化为 - 1
bfs()