60分求调

B3625 迷宫寻路

FOFTGSMOLL @ 2024-10-19 17:11:34

dfs算法

#include<cstdio>
char a1[110][110];
int n,m,map[110][110],vis[110][110],tmp;
int x[4]={0,0,1,-1},y[4]={-1,1,0,0};
void dfs(int x1,int y1){
    if(x1==n&&y1==m){
        tmp++;
        return;
    }
    for(int i=0;i<=3;i++){
        if(map[x1+x[i]][y1+y[i]]==1&&vis[x1+x[i]][y1+y[i]]==0){
            vis[x1+x[i]][y1+y[i]]=1;
            dfs(x1+x[i],y1+y[i]);
            vis[x1+x[i]][y1+y[i]]=0; 
        }
    }
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            scanf("%c",&a1[i][j]);
            if(a1[i][j]=='.'){
                map[i][j]=1;
            }else{
                map[i][j]=0;
            }
        }
    }
    dfs(n,m);
    if(tmp==0){
        printf("No");
    }else{
        printf("Yes");  
    }
}

|