当前位置:网站首页>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 .
边栏推荐
- CVE-2022-0847(提权内核漏洞)
- 迷宫问题(BFS记录路径)
- DDD understanding of Domain Driven Design
- New hybrid architecture iformer! Flexible migration of convolution and maximum pooling to transformer
- The IPO of Tian'an technology was terminated: Fosun and Jiuding were shareholders who planned to raise 350million yuan
- Hongshi electric appliance rushes to the Growth Enterprise Market: the annual revenue is 600million yuan. Liujinxian's equity was frozen by Guangde small loan
- Runmaide medical passed the hearing: Ping An capital was a shareholder with a loss of 630million during the year
- Verilog使用inout信号的方法
- On the routing tree of gin
- Discourse 新用户可插入媒体的数量
猜你喜欢

After 100 days, Xiaoyu built a robot communication community!! Now invite moderators!

Discourse 的信任级别

基于最小化三维NDT距离的快速精确点云配准

Meet webassembly again

【单片机】【让蜂鸣器发声】认识蜂鸣器,让蜂鸣器发出你想要的声音

Wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe

Tdengine connector goes online Google Data Studio store

专业“搬砖”老司机总结的 12 条 SQL 优化方案,非常实用!

Countdown to the conference - Amazon cloud technology innovation conference invites you to build a new AI engine!

英国考虑基于国家安全因素让Arm在伦敦上市
随机推荐
How can ordinary people make 1million yuan a year?
向量3(静态成员)
基础版现在SQL分析查询不能用了吗?
Scala language learning-06-differences between name passing parameters, value passing parameters and function passing parameters
Exploration and practice of dewu app data simulation platform
【单片机】【让蜂鸣器发声】认识蜂鸣器,让蜂鸣器发出你想要的声音
SDVO:LDSO+语义,直接法语义SLAM(RAL 2022)
加密市场进入寒冬,是“天灾”还是“人祸”?
天安科技IPO被终止:曾拟募资3.5亿 复星与九鼎是股东
我靠副业一年全款买房:那个你看不起的行业,未来十年很赚钱!
ORB_VI思想框架
Community article | mosn building subset optimization ideas sharing
跨界融合创意创新,助力提高文旅夜游影响力
New load balancing webclient CRUD
Hongshi electric appliance rushes to the Growth Enterprise Market: the annual revenue is 600million yuan. Liujinxian's equity was frozen by Guangde small loan
推進兼容適配,使能協同發展 GBase 5月適配速遞
Method of using inout signal in Verilog
Scala语言学习-05-递归和尾递归效率对比
I took a private job and earned 15250. Is it still necessary to do my main business?
微信小程序头像挂件制作