40分求助

B3625 迷宫寻路

BlackWuKong @ 2024-07-11 08:52:01

#include<bits/stdc++.h>
using namespace std;
int n,m,flag=0;
char a[105][105];
void work(int x,int y){
    if (x==n&&y==m){
        cout<<"Yes";
        flag=1;
        return ;
    }
    if (a[x+1][y]=='.') work(x+1,y),a[x+1][y]=1;
    if (a[x-1][y]=='.') work(x+1,y+1),a[x-1][y]=1;
    if (a[x][y+1]=='.') work(x+1,y),a[x][y+1]=1;
    if (a[x][y-1]=='.') work(x+1,y),a[x][y-1]=1;
}
int main(){
    cin>>n>>m;
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            cin>>a[i][j];
        }
    }
    work(1,1);
    if (flag==0) cout<<"No";
    return 0;
}

|