bfs迷宫

Posted lipu123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bfs迷宫相关的知识,希望对你有一定的参考价值。

链接:https://ac.nowcoder.com/acm/contest/338/B
Sleeping is a favorite of little bearBaby, because the wetness of Changsha in winter is too uncomfortable. One morning, little bearBaby accidentally overslept. The result of being late is very serious. You are the smartest artificial intelligence. Now little bearBaby  asks you to help him figure out the minimum time it takes to reach the teaching building.
The school map is a grid of n*m, each cell is either an open space or a building (cannot pass), and the bedroom of little bearBaby is at (1,1)—— the starting point coordinates.The teaching building is at (x, y)——the target point coordinates, he  can only go up, down, left or right, it takes 1 minute for each step. The input data ensures that the teaching building is reachable.

技术图片

 

链接:https://ac.nowcoder.com/acm/contest/338/B
来源:牛客网

输入描述:

The first line has two positive integers n, m , separated by spaces(1 <= n, m <= 100), n for the row, m for the column
Next there are two positive integers x, y, separated by spaces(1 <= x <= n, 1 <= y <= m) indicating the coordinates of the teaching building
Next is a map of n rows and m columns, 0 indicate a open space and 1 indicate a obstacles.

输出描述:

For each test case, output a single line containing an integer giving the minimum time little bearBaby takes to reach the teaching building, in minutes.
示例1

输入

复制
5 4
4 3
0 0 1 0
0 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1

输出

复制
7

说明

For the input example, you could go like this:
(1,1)-->(1,2)-->(2,2)-->(2,3)-->(2,4)-->(3,4)-->(4,4)-->(4,3),so the minimum time is 7.

备注:

First grid in the upper left corner is(1,1)

AC代码:
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
inline int read() {int x=0,f=1;char c=getchar();while(c!=-&&(c<0||c>9))c=getchar();if(c==-)f=-1,c=getchar();while(c>=0&&c<=9)x=x*10+c-0,c=getchar();return f*x;}
typedef long long ll;
const int maxn = 1e4+10;
struct node{
    int x,y;
    int st;
};

int dir[4][2]={-1,0,1,0,0,-1,0,1};
int vis[maxn][maxn];
int n,m,n1,m1;
char a[1000][1000];

int bfs(int u,int v){
    node tmp,nex;
    tmp.x=u;
    tmp.y=v;
    tmp.st=0;
    queue<node>q;
    q.push(tmp);
    while(!q.empty()){
        tmp=q.front();
        q.pop();
        for(int i=0;i<4;i++){
            nex.x=tmp.x+dir[i][0];
            nex.y=tmp.y+dir[i][1];
            nex.st=tmp.st+1;
            if(nex.x<0||nex.y<0||nex.x>=n||nex.y>=m||a[nex.x][nex.y]==1||vis[nex.x][nex.y]==1)continue;
            vis[nex.x][nex.y]=1;
            if(nex.x==n1-1&&nex.y==m1-1){
                return nex.st;
            }

            q.push(nex);
        }
    }
}
int main()
{

    cin>>n>>m>>n1>>m1;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>a[i][j];
        }
    }
    int ans=bfs(0,0);
    printf("%d",ans);
    return 0;
}

以上是关于bfs迷宫的主要内容,如果未能解决你的问题,请参考以下文章

通过迷宫问题简单学习DFS和BFS算法

通过迷宫问题简单学习DFS和BFS算法

BFS-迷宫最短路径问题

BFS(最短路径)—— 迷宫问题

使用BFS(广度优先搜索)解迷宫类问题

地下迷宫(bfs输出路径)