迷宫寻宝(BFS模板题)

洪尼玛今天准备去寻宝,在一个n*n (n行, n列)的迷宫中,存在着一个入口、一些墙壁以及一个宝藏。由于迷宫是四连通的,即在迷宫中的一个位置,只能走到与它直接相邻的其他四个位置(上、下、左、右)。现洪尼玛在迷宫的入口处,问他最少需要走几步才能拿到宝藏?若永远无法拿到宝藏,则输出-1。

Input

多组测试数据。

每组数据输入第一行为正整数n,表示迷宫大小。

接下来n行,每行包括n个字符,其中字符'.'表示该位置为空地,字符'#'表示该位置为墙壁,字符'S'表示该位置为入口,字符'E'表示该位置为宝藏,输入数据中只有这四种字符,并且'S'和'E'仅出现一次。

n≤1000

Output

输出拿到宝藏最少需要走的步数,若永远无法拿到宝藏,则输出-1。

#include<bits/stdc++.h>
using namespace std;
int ex,ey;
int n;
int fx[4][2]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};//走位方向数组 
int bz[20][20];//标志遍历情况数组 
char lk[20][20];//原始路况数组 
struct node//用结构体保存当前的位置x.y和当前所用步数step 
{
	int x;
	int y;
	int step;
};
int bfs(int c,int d)//广度优先搜索算法 
{
	queue<node>q;
	node now,temp,pre;
	pre.x=c;
	pre.y=d;
	pre.step=0;
	q.push(pre); 
	while(!q.empty())
	{
		now=q.front();
		q.pop();
		if(now.x==ex&&now.y==ey)//到达指定地点 
		{
			return 1;
		}
		for(int i=0;i<4;i++)//遍历四个可走的方向 
		{
			temp.x = now.x+fx[i][0];
            temp.y = now.y+fx[i][1];
            temp.step = now.step + 1;
            if(temp.x>=1&&temp.y>=1&&temp.x<=n&&temp.y<=n&&lk[temp.x][temp.y]!='#'&&bz[temp.x][temp.y]==0)//判断当前路段是否可走 
            {
            	q.push(temp);
				bz[temp.x][temp.y]=1; //可走标志为1表示已经遍历过了 
			}
		}
	}
	return 0;
}


int main()
{
	int t=0;
    cin>>t;
    while(t--)
    {
    	//int n=0;
    	cin>>n;
    	int a=0,b=0;
    	for(int i=1;i<=n;i++)
    	{
    		for(int j=1;j<=n;j++)
    		{
    			cin>>lk[i][j];
    			if(lk[i][j]=='s')//找起点 
    			{
    				a=i;
    				b=j;
				}
				if(lk[i][j]=='e')//找终点 
				{
					ex=i;
					ey=j;
				}
			}
		}
		int flag=0;
		flag=bfs(a,b);
		if(flag==0)
		{
			cout<<"NO"<<endl;
		}
		else 
		{
			cout<<"YES"<<endl;
		}
	}
	return 0;
 } 
### 迷宫问题 BFS 算法模板 以下是基于引用内容构建的一个完整的迷宫问题 BFS 算法模板。该模板适用于求解从起点 `(sx, sy)` 到终点 `(ex, ey)` 的最短路径长度。 #### 数据结构定义 为了实现 BFS,通常需要一个队列来存储当前状态节点的信息。这里可以采用 `struct` 定义节点的状态,并通过数组模拟队列操作[^2]。 ```c++ #include <bits/stdc++.h> using namespace std; // 定义方向向量(上下左右) const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; // 定义节点结构体 struct Node { int x, y, step; Node(int _x, int _y, int _step) : x(_x), y(_y), step(_step) {} }; bool isValid(int nx, int ny, int n, int m, vector<vector<int>> &maze, vector<vector<bool>> &visited) { if (nx >= 0 && nx < n && ny >= 0 && ny < m && maze[nx][ny] == 0 && !visited[nx][ny]) { return true; } return false; } ``` #### 主函数逻辑 在主函数中初始化队列并执行 BFS 遍历过程。每次扩展时判断新位置是否合法且未访问过,如果是,则将其加入队列继续遍历[^3]。 ```c++ int bfs(int sx, int sy, int ex, int ey, vector<vector<int>> &maze) { int n = maze.size(); int m = maze[0].size(); // 初始化 visited 数组防止重复访问 vector<vector<bool>> visited(n, vector<bool>(m, false)); // 创建队列并将起始点入队 queue<Node> q; q.push(Node(sx, sy, 0)); visited[sx][sy] = true; while (!q.empty()) { Node current = q.front(); q.pop(); // 如果到达目标点则返回步数 if (current.x == ex && current.y == ey) { return current.step; } // 尝试四个方向移动 for (int i = 0; i < 4; ++i) { int nx = current.x + dx[i]; int ny = current.y + dy[i]; if (isValid(nx, ny, n, m, maze, visited)) { visited[nx][ny] = true; q.push(Node(nx, ny, current.step + 1)); } } } // 若无法抵达终点,返回 -1 return -1; } ``` #### 使用示例 下面是一个简单的测试案例展示如何调用上述 BFS 函数: ```c++ int main() { // 输入迷宫地图 vector<vector<int>> maze = { {0, 1, 0, 0}, {0, 1, 0, 1}, {0, 0, 0, 0}, {0, 1, 1, 0} }; int sx = 0, sy = 0; // 起点坐标 int ex = 3, ey = 3; // 终点坐标 int result = bfs(sx, sy, ex, ey, maze); cout << "Minimum steps required: " << result << endl; return 0; } ``` 此代码片段展示了如何利用 BFS 来计算迷宫中最短路径所需的最少步数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值