当前位置:网站首页>[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;
}
边栏推荐
- 48:第五章:开发admin管理服务:1:创建子工程【imooc-news-dev-service-admin】,管理服务模块;
- Direct exchange
- Distance IOU loss: faster and better learning for bounding box regression
- 通用分页功能
- [C language] structure, enumeration and union
- 【Error】TypeError: expected str, bytes or os.PathLike object, not int
- IE盒模型和标准盒模型
- pip报错Could not find a version that satisfies the...No matching distribution
- UPC 2022暑期个人训练赛第12场(B 组合数)
- USB基础
猜你喜欢

Tensorflow2.x actual combat series softmax function

一文带你了解什么是TypeScript

Compose Canvas饼图效果绘制

灰色预测(MATLAB)

FreeRTOS个人笔记-挂起/解挂任务

anchor free yolov1

拼多多APP商品详情接口获取activity_id值(拼多多activity_id接口)

AutoCAD基本操作

Solve data functions should return an object (property "visible" must be accessed with "$data.visible")
![[C language] structure, enumeration and union](/img/18/3d9c511950cbcbd109df3104fbe248.png)
[C language] structure, enumeration and union
随机推荐
pairwise的使用
mysql如何查询不在数据库里的数据?
Eureka notes
Deep learning convolutional neural network paper study alexnet
微机原理与技术接口随堂练习
COPU副主席刘澎:中国开源在局部领域已接近或达到世界先进水平
anchor free yolov1
学习笔记7--交通环境行为预测
Frequently asked questions about MySQL
TS encapsulates the localstorage class to store information
Direct exchange
Bag of tricks for image classification "with convolutional neural networks"
Weisfeiler-Lehman图同构测试及其他
Squeeze and incentive networks
How does MySQL query data that is not in the database?
Leetcode-67. binary sum
Eureka笔记
启牛商学院上面开户安全不
Advanced authentication of uni app [Day12]
go run,go build,go install有什么不同