题解 | #走迷宫#

走迷宫

https://www.nowcoder.com/practice/e88b41dc6e764b2893bc4221777ffe64

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,k,l,x,y,a,b,ans;
char ch[1010][1010];
bool book[1010][1010];
int ne[4][2]={1,0,-1,0,0,1,0,-1};
struct node{
    int x,y,z;
};
void slove(){
    cin>>n>>m>>a>>b>>x>>y;
    ans=1e18;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>ch[i][j];
        }
    }
    queue<node>q;
    q.push({a,b,0});
    book[a][b]=true;
    while(q.size()){
        node t1=q.front();
        q.pop();
        if(t1.x==x && t1.y==y){
            ans=min(ans,t1.z);
        }
        for(int i=0;i<4;i++){
            int xx=t1.x+ne[i][0];
            int yy=t1.y+ne[i][1];
            if(xx>=1 && xx<=n && yy>=1 && yy<=m && !book[xx][yy] && ch[xx][yy]=='.'){
                q.push({xx,yy,t1.z+1});
                book[xx][yy]=true;
            }
        }
    }
    if(ans!=1e18) cout<<ans<<'\n';
    else cout<<-1<<'\n';
}
signed main() {
    int a, b;
    int T=1;
    while(T--){
        slove();
    }
}
// 64 位输出请用 printf("%lld")

#广搜模板#
全部评论

相关推荐

苗条的伊泽瑞尔最喜欢...:同28届被压力了,电科✌就不能去卷算法吗?把Java留给我们双非卷
投递快手等公司10个岗位
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务