全T求条

B3625 迷宫寻路

hiebb @ 2024-08-14 18:30:11

#include<bits/stdc++.h>
using namespace std;
int n,m;
char c;
bool f[110][110],ff=1;
void dfs(int x,int y){
    if(x<0||y<0||x>n||y>m||f[x][y]) return;
    if(x==n&&y==m){
        ff=0;
        cout<<"YES";
        return;
    }
    f[x][y]=1;
    if(!f[x-1][y]&&ff) dfs(x-1,y);
    if(!f[x+1][y]&&ff) dfs(x+1,y);
    if(!f[x][y-1]&&ff) dfs(x,y-1);
    if(!f[x][y+1]&&ff) dfs(x,y+1);
    f[x][y]=0;
    return;
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            cin>>c;
            if(c=='#') f[i][j]=1;
            else f[i][j]=0;
        }
    dfs(1,1);
    if(ff) cout<<"NO";
    return 0;
}

by Adorable_ @ 2024-09-27 14:21:37

@shtian okok懂啦,谢谢大佬(360°鞠躬


上一页 |