迷宫BFS

给定一个大小为N*M的迷宫。迷宫由通道和墙壁组成,每一步可以向相邻的上下左右四格的通道移动。请求出从起点到终点所需的最小步数。如果不能到达,输出“不能走到那里”。(N,M<=50,起点,终点分别用S,G表示)

输入样例:N=5,M=5
#S###
..##.
#.###
..###
..G## 

输出:5
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <list>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <cmath>
using namespace std;

const int maxn = 100 + 10;
int n,m;
int sx,sy,ex,ey;
int dx[]={1,0,-1,0}; 
int dy[]={0,1,0,-1};
char mg[maxn][maxn];
bool vis[maxn][maxn];


struct node
{
	int x,y;
	int step;
};
void bfs()
{
	node p;
	p.x = sx; p.y = sy;
	p.step = 0;
	queue<node> q;
	q.push(p);
	vis[sx][sy]=1;
	while(!q.empty())
	{
		node tmp = q.front();
		q.pop();
		if(tmp.x == ex && tmp.y == ey)
		{
			cout<<"最短路径:"<<tmp.step<<endl;
			return; 
		}
		for(int i=0;i<4;i++)
		{
			int xx = tmp.x + dx[i];
			int yy = tmp.y + dy[i];
			if(mg[xx][yy]!='#'&&xx>0&&yy>0&&xx<=n&&yy<=m&&!vis[xx][yy])
			{
				node tp;
				tp.x = xx;
				tp.y = yy;
				tp.step = tmp.step + 1;
				vis[xx][yy] = 1;
				q.push(tp);
			}	
		}
	}
	cout<<"不能走到耶。\n";
}

int main() 
{
	while(scanf("%d%d",&n,&m) == 2 && n && m)
	{
		memset(vis,0,sizeof(vis));
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				cin>>mg[i][j];
				if(mg[i][j]=='S')
					{sx=i;sy=j;}
				else if(mg[i][j]=='G')
					{ex=i;ey=j;}
			}	
		}
		bfs();
	}
}
### 关于 C++ 实现迷宫 BFS 算法 #### 结构体定义 为了表示迷宫中的每一个位置及其状态,在程序中通常会创建一个结构体 `Node` 来存储当前节点的位置 `(x, y)` 其父节点的位置 `(px, py)`。这有助于追踪路径,以便最终能够回溯找到完整的解决方案。 ```cpp struct Node { int x; int y; int px; int py; }; ``` 此部分描述了如何构建用于保存迷宫坐标的节点信息[^2]。 #### 初始化队列与访问标记数组 广度优先搜索依赖于队列的数据结构来管理待探索的节点列表,并使用布尔类型的二维数组记录哪些位置已经被访问过,防止重复遍历同一地点造成死循环或者不必要的计算资源浪费。 #### 主要逻辑流程 当处理迷宫问题时,BFS 的核心在于按照层次顺序逐层扩展相邻未被访问过的单元格直到遇到目标终点为止;如果成功抵达,则返回所经过的距离即为最短路径长度。具体操作包括但不限于读取输入尺寸、障碍物分布以及起始结束坐标等参数设置工作。 #### 完整代码示例 下面给出一段基于上述原理编写的完整 C++ 代码片段: ```cpp #include <iostream> #include <queue> using namespace std; const int MAXN = 105; bool vis[MAXN][MAXN]; int maze[MAXN][MAXN], dist[MAXN][MAXN]; // 方向枚举 dx[] dy[] static const int dir[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; struct Node { int x; int y; int step; Node(int _x=0,int _y=0,int s=0):x(_x),y(_y),step(s){} }; void bfs(Node start, Node end) { queue<Node> q; memset(vis, false, sizeof(vis)); while (!q.empty()) q.pop(); q.push(start); vis[start.x][start.y] = true; dist[start.x][start.y]=0; while(!q.empty()){ Node now=q.front();q.pop(); if(now.x==end.x && now.y==end.y){ cout << "Minimum steps required: "<<now.step<<endl; return ; } for (int i = 0; i < 4 ; ++i ) { int nx = now.x + dir[i][0]; int ny = now.y + dir[i][1]; if(nx>=0&&nx<maze[0][0]&&ny>=0&&ny<maze[0][1]){ if(maze[nx][ny]==1&&!vis[nx][ny]){ vis[nx][ny]=true; q.push({nx ,ny ,now.step+1}); } } } } } int main() { int n,m,sx,sy,gx,gy; cin>>n>>m; for(int i=0;i<n;++i) for(int j=0;j<m;++j) cin >>maze[i][j]; cin>>sx>>sy>>gx>>gy; bfs(Node(sx-1,sy-1,0),Node(gx-1,gy-1)); return 0; } ``` 这段代码实现了从给定起点至指定目的地之间寻找最短路径的功能,并输出最小移动次数作为结果[^3]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值