当前位置:网站首页>Walk the Maze BFS
Walk the Maze BFS
2022-08-03 22:47:00 【Little moxa cuisine dishes】
题目描述
给定一个n*m的二维整数数组,用来表示一个迷宫,数组中只包含0或1,其中0表示可以走的路,1表示不可通过的墙壁.
最初,有一个人位于左上角(1, 1)处,已知该人每次可以向上、下、左、右任意一个方向移动一个位置.
请问,该人从左上角移动至右下角(n, m)处,至少需要移动多少次.
数据保证(1, 1)处和(n, m)处的数字为0,且一定至少存在一条通路.
输入格式
第一行包含两个整数n和m.
接下来n行,每行包含m个整数(0或1),表示完整的二维数组迷宫.
输出格式
输出一个整数,表示从左上角移动至右下角的最少移动次数.
数据范围
1≤n,m≤100
样例
5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 0
0 1 1 1 0
0 0 0 1 0
样例输出
8
解题思路:
没有什么特别的技巧,Using breadth-first search traversal violence can be.
The specific way how to search,Please look at this picture below:

具体的代码实现
I used to queue here,Which I would, in the form of handwritten implementation queue,与直接使用C++ STL的queue To write the code to solve this problem
数组模拟队列实现:
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 110;
typedef pair<int, int> PII;
int n, m;
int g[N][N];//存放地图
int d[N][N];//存 每一个点到起点的距离
PII q[N * N];//手写队列
int bfs()
{
int hh = 0, tt = 0;
q[0] = {
0, 0};
memset(d, - 1, sizeof d);//距离初始化为- 1表示没有走过
d[0][0] = 0;//表示起点走过了
int dx[4] = {
-1, 0, 1, 0}, dy[4] = {
0, 1, 0, -1}; //x 方向的向量和 y The direction of the vector consisting of、右、下、左
//If didn't quite understand the above control direction with little skill,Can on scratch paper to[0,0]为例,以(x,y) = (-1,0) One-to-one to implement the following the up and down or so,Is very clear
while(hh <= tt)//队列不空
{
PII t = q[hh ++ ];//取队头元素
for(int i = 0; i < 4; i ++ ) //枚举4个方向
{
int x = t.first + dx[i], y = t.second + dy[i];//x表示沿着此方向走会走到哪个点
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;//到起点的距离
q[ ++ tt ] = {
x, y};//新坐标入队
}
}
}
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;
}
C++ STL queue
#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())//队列不为空
{
PII t = q.front();//取队头元素
q.pop();//出队
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;//当前点到起点的距离
q.push({
x, y});//将新坐标入队
}
}
}
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;
}
打印路径
#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];
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;
q[++ tt] = {
x, y};
}
}
}
int x = n - 1, y = m - 1;
while(x || y)//有一个不d等于0
{
cout << x << ' ' << y << endl;
PII t = Prev[x][y];
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;
}
边栏推荐
- Interpretation of ML: A case of global interpretation/local interpretation of EBC model interpretability based on titanic titanic rescued binary prediction data set using interpret
- What is the difference between the generator version and the viewer version?
- 如何基于WPF写一款数据库文档管理工具(二)
- ML之interpret:基于titanic泰坦尼克是否获救二分类预测数据集利用interpret实现EBC模型可解释性之全局解释/局部解释案例
- Shell编程的条件语句
- MiniAPI of .NET6 (14): Cross-domain CORS (Part 1)
- 为什么我们需要回调
- Embedded Systems: Clocks
- HDU 5655 CA Loves Stick
- 【MySQL进阶】数据库与表的创建和管理
猜你喜欢
![[N1CTF 2018]eating_cms](/img/09/3599d889d9007eb45c6eab3043f0c4.png)
[N1CTF 2018]eating_cms

"Digital Economy Panorama White Paper" Financial Digital User Chapter released!

投资性大于游戏性 NFT游戏到底是不是门好生意

代码随想录笔记_动态规划_416分割等和子集

complete binary tree problem

2022-08-03 Oracle executes slow SQL-Q17 comparison

Cisco ike2 IPSec configuration

ML之yellowbrick:基于titanic泰坦尼克是否获救二分类预测数据集利用yellowbrick对LoR逻辑回归模型实现可解释性(阈值图)案例

软测人每个阶段的薪资待遇,快来康康你能拿多少?

Click the icon in Canvas App to generate PDF and save it to Dataverse
随机推荐
嵌入式系统:概述
《数字经济全景白皮书》金融数字用户篇 重磅发布!
noip初赛
封装、包、访问权限修饰符、static变量
云平台建设解决方案
冰河又一MySQL力作出版(文末送书)!!
Cloud platform construction solutions
如何基于WPF写一款数据库文档管理工具(二)
Take an example of a web worker
为什么我们需要回调
CAS:178744-28-0,mPEG-DSPE,DSPE-mPEG,甲氧基-聚乙二醇-磷脂酰乙醇胺供应
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
Makefile
【bug】汇总Elipse项目中代码中文乱码解决方法!
【day1】
Cisco ike2 IPSec配置
Golang第二章:程序结构
Nine ways to teach you to read the file path in the resources directory
Lift, Splat, Shoot: Encoding Images from Arbitrary Camera Rigs by Implicitly Unprojecting to 3D 论文笔记
获国际权威认可 | 云扩科技入选《RPA全球市场格局报告,Q3 2022》