当前位置:网站首页>[31. Maze walking (BFS)]
[31. Maze walking (BFS)]
2022-07-23 16:57:00 【Little silly bird_ coding】
- When asked
When the shortest path, alsoThe weight of the edge is 1when Use BFS( Breadth first search .)

Ideas
use
g[n][m]Store maps ,d[n][m]Store starting point to n,m Distance of .From the starting point, breadth first traverse the map .
When the map is traversed , The distance from the starting point to each point is calculated , Output
d[n][m]that will do .
Figure explanation


Reasons for using queues
- Because when you reach a point , There are four choices , We must walk in four directions at the same time , But the computer can only perform one step at a time , So put the directions to be processed into the queue in turn
- Go to the corresponding point every time , Just take it out of the queue , Look in which direction
subject

Method 1: Simulation queue
#include <iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef pair<int, int> PII;
const int N = 110;
int n, m;
int g[N][N]; // Stored is a map
int d[N][N]; // Save the distance from this point to the starting point
PII q[N * N]; // Handwriting queue
int bfs()
{
int hh = 0, tt = 0; // Define team head and tail
q[0] = {
0, 0}; // To initialize
memset(d, -1, sizeof(d)); // The distance is initialized to - 1 It means you haven't walked
d[0][0] = 0; // It means the starting point has passed
int dx[4] = {
-1, 0, 1, 0}, dy[4] = {
0, 1, 0, -1}; // Definition x Vector sum of directions y Vector of direction
while (hh <= tt) // The queue is not empty
{
PII t = q[hh ++]; // Take out the team leader element
for (int i = 0; i < 4; i ++) // Enumerate four directions
{
int x = t.first + dx[i], y =t.second + dy[i]; x Indicates the point you will reach when you walk in this direction
if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 0 && d[x][y] == -1)// Within the boundary And it's an open space for walking And I haven't walked before
{
d[x][y] = d[t.first][t.second] + 1; // The distance to the starting point
q[ ++ tt] = {
x, y}; // New coordinates join the team
}
}
}
return d[n - 1][m - 1]; Output the distance between the lower right corner and the starting point
}
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;
}
Method 2:STL
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int N = 110;
typedef pair<int, int> PII;
int n, m;
int g[N][N], d[N][N];
int bfs()
{
queue< pair<int, int> > q;
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())// The queue is not empty
{
PII t = q.front();// Take the team leader element
q.pop();// Out of the team
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;// The distance from the current point to the starting point
q.push({
x, y});// New team
}
}
}
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;
}
return d[n-1][m-1] Is the minimum path
- When I first found this point ( The shortest distance ) This point is marked If you visit this point next time, you won't be able to visit ( So don't jump out of the loop when you find it )
- After access is unavailable , The elements in the queue will pop up in turn , Exit the loop when the queue is empty .
Print path
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 110;
typedef pair<int, int> PII;
PII q[N*N],Prev[N][N]; // Open more arrays , Record the previous point
int g[N][N], d[N][N];
int n, m;
int bfs()
{
int hh = 0, tt = 0;
q[0] = {
0, 0};
memset(d, -1, sizeof d);
int dx[4] = {
-1, 0, 1, 0}, dy[4] = {
0, 1, 0, -1};
d[0][0] = 0;
while(hh <= tt)
{
PII t = q[hh ++ ];
for(int i = 0; i < 4; i ++ )
{
int x = dx[i] + t.first, 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;
Prev[x][y] = t; // Store the previous point
q[++ tt] = {
x, y};
}
}
}
int x = n - 1, y = m - 1;
while(x || y)// One is not d be equal to 0
{
cout << x << ' ' << y << endl; // Print the current point
PII t = Prev[x][y]; // Look for the previous point , Continue printing
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;
}
边栏推荐
猜你喜欢

无心剑英汉双语诗006.《致爱妻》

Bag of Tricks for Image Classification with Convolutional Neural Networks(卷积神经网络在图像分类中的技巧)

Squeeze and incentive networks

【TensorFlow】检测TensorFlow GPU是否可用

Scale Match for Tiny Person Detection

Notes on Microcomputer Principle and technical interface

使用“soup.h1.text”爬虫提取标题会多一个\
![[C language] structure, enumeration and union](/img/18/3d9c511950cbcbd109df3104fbe248.png)
[C language] structure, enumeration and union

一款非常棒的开源微社区轻论坛类源码

深度学习卷积神经网络论文研读-AlexNet
随机推荐
熵权法优化TOPSIS(MATLAB)
学习MySQL这一篇就够了
系统内存介绍和内存管理
It's not safe to open an account at qiniu business school
pairwise的使用
Wechat applet class binding, how to bind two variables
精确的目标检测中定位置信度的获取
O3DF执行董事Royal O’Brien:开源没有边界,所有共享的声音都会变成实际方向
英特尔nuc能代替主机吗_终于圆满了!最新款的Intel NUC迷你主机上线
Nifi 1.16.3 cluster setup +kerberos+ user authentication
TOPSIS法(MATLAB)
学习笔记7--交通环境行为预测
pwn入门(3)堆
tensorflow一层神经网络训练手写体数字识别
Numpy 数据分析基础知识第一阶段(NumPy基础)
UPC 2022暑期个人训练赛第12场(B 组合数)
YOLOV7
FIO performance testing tool
微机原理与技术接口笔记
AutoCAD基本操作