当前位置:网站首页>Maze problem (BFS record path)
Maze problem (BFS record path)
2022-06-22 15:47:00 【Stephen_ Curry___】
BFS Walking in a maze is generally two problems , The first is to ask the shortest route , The second is to ask the shortest path length , Today, I'd like to introduce you to the first output maze path .
Let's start with a classic example :
Define a two-dimensional array :
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,
};
It means a maze , Among them 1 Wall ,0 Means the way to go , You can only walk horizontally or vertically , Don't walk sideways , Ask the program to find the shortest route from the top left corner to the bottom right corner .
Output the shortest route from the upper left corner to the lower right corner of the maze
SampleOutput
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
What is your first thought when you see the problem ? I was just trying to use BFS How to record the path if you write it , because BFS It's written like a queue ( Join the team , One side out of the team ), Thought for a long time , I also found a lot of articles on the Internet to read , But I can't find the answer I want , Looking at many articles, I suddenly got inspiration , Thought of how to record the path .
AC The code and comments are as follows :
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 100;
const int M = 100005;
struct node {
int x, y;
}w[M];
int mp[N][N];// Save the map
int ans[M];// Used to record the number of steps taken
// Use vectors to search up, down, left and right
int dx[4] = {
0,1,-1,0 };
int dy[4] = {
1,0,0,-1 };
void dfs(int u)
{
// Only uninitialized ans[0]=0 Satisfy ans[u]==u, Everything else has ans[u]>u;
if(ans[u]==u)
{
printf("(0, 0)\n");
return ;
}
dfs(ans[u]);
printf("(%d, %d)\n",w[u].x,w[u].y);
}
void bfs(int x, int y)
{
int head = 0, tail = 0;
int flag=0;//flag It is used to mark whether the destination has been reached
w[tail++] = {
x,y };// Simulation queue
while (head != tail)
{
for (int i = 0; i < 4; i++)// Guang Shu , Expand in four directions
{
int nx = w[head].x + dx[i];
int ny = w[head].y + dy[i];
// Judge whether the point meets the conditions
if (nx < 0 || nx >= 5 || ny < 0 || ny >= 5||mp[nx][ny]==1)continue;
// If the conditions are met, join the team at this point
w[tail] = {
nx,ny };
// Here we use the initial time head and tail Unequal to save steps
// Let's go tail The coordinates of the previous point of the step are (w[head].x, w[head].y)
ans[tail]=head;// Record where the previous point of the point is
tail++;
if(nx==4&&ny==4)
{
flag=1;
break;
}
}
if(flag==1)
{
dfs(tail-1);// Start the output operation
break;
}
head++;
}
}
int main()
{
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
cin>>mp[i][j];
bfs(0,0);// Start from the starting point and search widely
}
The above is what I want to introduce today BFS The problem of finding the shortest route , If you have your own views on the content , Please give me your advice .
边栏推荐
- Reconstruction practice of complex C-end project of acquisition technology
- 向量1(类和对象)
- Scala语言学习-06-传名参数、传值参数、传函数参数的区别
- 标准化、最值归一化、均值归一化应用场景的进阶思考
- C # implements insertion sorting
- Ultimate efficiency is the foundation for the cloud native database tdsql-c to settle down
- C language learning -18-makefile file writing examples and how to generate and call dynamic libraries
- Found several packages [runtime, main] in ‘/usr/local/Cellar/go/1.18/libexec/src/runtime;
- “软件定义世界,开源共筑未来” 2022开放原子全球开源峰会7月底即将开启
- Exploration and practice of dewu app data simulation platform
猜你喜欢

Runmaide medical passed the hearing: Ping An capital was a shareholder with a loss of 630million during the year

天安科技IPO被终止:曾拟募资3.5亿 复星与九鼎是股东

模板特例化 template<>

Exploration and practice of dewu app data simulation platform

建议自查!MySQL驱动Bug引发的事务不回滚问题,也许你正面临该风险!

【题目精刷】2023禾赛-FPGA

Ultimate efficiency is the foundation for the cloud native database tdsql-c to settle down

C# 实现插入排序

社区文章|MOSN 构建 Subset 优化思路分享

Database connection pool: implementation of connection pool function point
随机推荐
"Forget to learn again" shell process control - 38. Introduction to while loop and until loop
蓝桥杯2019年国赛最长子序列
再次认识 WebAssembly
我靠副业一年全款买房:那个你看不起的行业,未来十年很赚钱!
The summary of high concurrency experience under the billion level traffic for many years is written in this book without reservation
How MySQL modifies the storage engine to InnoDB
大佬们 2.2.1cdc 监控sqlsever 只能拿到全量的数据 后期增量的数据拿不到 咋回事啊
The bank card identification function of Huawei machine learning service enables bank card identification and binding with one click
A simple understanding of hill ordering
ML笔记-matrix fundamental, Gradient Descent
Using virtual serial port to debug serial port in keil MDK
On the routing tree of gin
What happened to those who didn't go to college
又可以这样搞nlp(分类)
After 17 years, Liu Yifei became popular again: ordinary people don't want to be eliminated, but they also need to understand this
建议自查!MySQL驱动Bug引发的事务不回滚问题,也许你正面临该风险!
推進兼容適配,使能協同發展 GBase 5月適配速遞
还可以这样搞nlp(分类)
ROS2前置基础教程 | 小鱼教你用CMake依赖查找流程
乱解码nlp