10pts求调 悬关

B3625 迷宫寻路

__Dist__ @ 2023-07-10 20:31:11

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,m;
char ch[105][105];
bool vis[105][105];
bool dis[105][105];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
queue<pair<ll,ll> > q;
bool bfs(int x,int y){
    q.push(make_pair(x,y));
    vis[x][y]=1;
    while(q.size()){
        x=q.front().first;
        y=q.front().second;
        q.pop();
        if(x<1||x>n||y<1||y>m) continue;
        vis[x][y]=1;
        //cout<<x<<' '<<y<<endl;
        if(x==n&&y==m) return true;
        for(int i=0;i<4;i++){
            int xx=x+dx[i];
            int yy=y+dy[i];
            if(dis[xx][yy]&&vis[xx][yy]==0){
                q.push(make_pair(xx,yy));
            }
        }
    }
    if(x==n&&y==m) return true;
    return false;
} 
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            cin>>ch[i][j],dis[i][j]=(ch[i][j]=='.');

    if(bfs(1,1)) puts("Yes");
    else puts("No");
    return 0;
}

by lzg_070506 @ 2023-07-10 20:38:39

建议在入队时就标记该点已走,不然会重点。


by __Dist__ @ 2023-07-11 08:21:09

@lzg_070506 谢谢大佬!Orz!


|