当前位置:网站首页>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 .
边栏推荐
- 鸿世电器冲刺创业板:年营收6亿 刘金贤股权曾被广德小贷冻结
- 还整成这样
- 小程序开发----自定义有效期缓存
- Development status of full color LED display
- 向量1(类和对象)
- The IPO of Tian'an technology was terminated: Fosun and Jiuding were shareholders who planned to raise 350million yuan
- After 17 years, Liu Yifei became popular again: ordinary people don't want to be eliminated, but they also need to understand this
- 二分查找(整数二分)
- How MySQL modifies the storage engine to InnoDB
- (pytorch进阶之路二)word embedding 和 position embedding
猜你喜欢
随机推荐
【题目精刷】2023禾赛-FPGA
Please, don't be brainwashed. This is the living reality of 90% of Chinese people
(pytorch进阶之路二)word embedding 和 position embedding
Rosbag使用命令
Ml notes matrix fundamental, gradient descent
【newman】postman生成漂亮的测试报告
小白操作Win10扩充C盘(把D盘内存分给C盘)亲测多次有效
洛谷P2466 [SDOI2008] Sue 的小球 题解
How MySQL modifies a field to not null
TDengine 连接器上线 Google Data Studio 应用商店
GBASE现身说 “库” 北京金融科技产业联盟创新应用专委会专题培训
向量6(继承)
FreeRTOS task priority and interrupt priority
新版负载均衡WebClient CRUD
Keil simulation and VSPD
向量3(静态成员)
标准化、最值归一化、均值归一化应用场景的进阶思考
HMS core news industry solution: let technology add humanistic temperature
Advanced thinking on application scenarios of standardization, maximum normalization and mean normalization
向量2(友元及拷贝构造)









