当前位置:网站首页>[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;
}
边栏推荐
- 网络协议与攻击模拟:wireshark使用、ARP协议
- Satisfiability of the equation of leetcode
- JS if the decimal is 0, subtract it, not keep it
- UPC 2022暑期个人训练赛第12场(B 组合数)
- Solve data functions should return an object (property "visible" must be accessed with "$data.visible")
- 48:第五章:开发admin管理服务:1:创建子工程【imooc-news-dev-service-admin】,管理服务模块;
- go run,go build,go install有什么不同
- AutoCAD基本操作
- 灰色关联分析(MATLAB)
- 精确的目标检测中定位置信度的获取
猜你喜欢

TS encapsulates the localstorage class to store information

go run,go build,go install有什么不同

General paging function

NodeJs实现token登录注册(KOA2)

The first stage of basic knowledge of numpy data analysis (numpy Foundation)

RISC-V基金会董事谭章熹:RISC-V,从边缘逐渐向中央扩展

anchor free yolov1

卷积神经网络模型之——GoogLeNet网络结构与代码实现

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

Chen Wei, head of CPU technology ecology of Alibaba pingtouge: the development road of pingtouge
随机推荐
卷积神经网络模型之——GoogLeNet网络结构与代码实现
僧多粥少?程序员要怎样接私活才能有效提高收入?
Direct exchange
一款非常棒的开源微社区轻论坛类源码
It's not safe to open an account at qiniu business school
What are the principal guaranteed financial products with an annual interest rate of about 6%?
主成分分析(MATLAB)
pwn入门(3)堆
FreeRTOS个人笔记-延时函数
How to buy financial products with a return of more than 6%?
银河证券网上开户,手机上开户安不安全
浏览器同源策略
C语言基础篇 —— 2-5 指针与函数知识点
How does MySQL query data that is not in the database?
Bag of tricks for image classification "with convolutional neural networks"
目前有哪些年利率6%左右的保本理财产品?
Four cores of browser
【Error】TypeError: expected str, bytes or os.PathLike object, not int
PMP每日一练 | 考试不迷路-7.23
JS if the decimal is 0, subtract it, not keep it