BFS第一次扩展到终点时即可求得最短路
AcWing 1076. 迷宫问题
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int bx[4] = {
0,0,1,-1};
int by[4] = {
1,-1,0,0};
int n;
struct node
{
int px;
int py;
}pre[1010][1010];
int a[1010][1010];
bool st[1010][1010];
void bfs(int x, int y)
{
queue<pair<int, int>> q;
q.push({
x, y});
st[x][y] = true;
while(!q.empty())
{
auto t = q.front();
q.pop();
int nowx = t.first, nowy = t.second;
if(nowx == 1 && nowy == 1)break;
for(int i = 0; i < 4; i ++ )
{
int nx = nowx + bx[i], ny = nowy +

这篇博客探讨了如何利用广度优先搜索(BFS)算法解决迷宫问题和寻找最短路径。在AcWing1076.迷宫问题中,通过BFS从起点到终点扩展,首次到达终点时即找到最短路径。在188.武士风度的牛和1100.抓住那头牛问题中,同样应用BFS来计算从起点到目标点的最短距离。这些题目展示了BFS在不同场景下的应用,包括在二维矩阵中寻找路径和在整数序列中查找目标元素的最短路径。
最低0.47元/天 解锁文章
1937

被折叠的 条评论
为什么被折叠?



