40分玄关

B3625 迷宫寻路

LINYUHENG2 @ 2024-12-21 16:47:34


#include <bits/stdc++.h>
using namespace std;
int n,m;
char c[1005][1005];
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
int vis[100][100];
queue<int> qx,qy;

void bfs(){
    qx.push(1);
    qy.push(1);
    while(!qx.empty()){
        int x=qx.front();
        int y=qy.front();
        qx.pop();
        qy.pop();
        if(x==n&&y==m){
            cout<<"Yes";
            return ;
        }
        for(int i=0;i<4;i++){
            int next_x=dir[i][0];
            int next_y=dir[i][1];
            if(next_x>=1&&next_x<=n&&next_y>=1&&next_y<=m&&!vis[next_x][next_y]&&c[next_x][next_y]!='#'){
                vis[next_x][next_y]=1;
                qx.push(next_x);
                qy.push(next_y);
            }
        }
    }
    cout<<"No";
    return ;
}

int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>c[i][j];
        }
    }
    bfs();
    return 0;
}

by sh_Andy @ 2024-12-21 17:15:29

int next_x=x+dir[i][0];
int next_y=y+dir[i][1];

|