码迷,mamicode.com
首页 > 其他好文 > 详细

bfs迷宫

时间:2020-01-10 18:38:50      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:main   for   pass   positive   getch   cat   printf   inf   using   

链接: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迷宫

标签:main   for   pass   positive   getch   cat   printf   inf   using   

原文地址:https://www.cnblogs.com/lipu123/p/12177507.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!