蒟蒻dfs90分求调(玄关)

B3625 迷宫寻路

springisnotcoming @ 2023-09-02 14:37:40

#include <iostream>
using namespace std;

int n,m;
char a[120][120];
bool vis[120][120]={{0}},ma[120][120],flag;

void dfs(int x,int y){
    if(x==n&&y==m){
        flag=1;
        return;
    }
    if(x<=0||y<=0||x>n||y>m)return;
    if(vis[x][y])return;
    vis[x][y]=1;
    if(!ma[x+1][y])dfs(x+1,y);
    if(!ma[x-1][y])dfs(x-1,y);
    if(!ma[x][y+1])dfs(x,y+1);
    if(!ma[x][y-1])dfs(x,y-1);
}

int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        for(int j=1;j<=n;j++){
            ma[i][j]=(a[i][j-1]=='#')?1:0;
        }
    }
    dfs(1,1);
    if(flag)cout<<"Yes";
    else cout<<"No";
    return 0;
}

by BugGod @ 2023-09-02 14:47:44

@spring_is_coming 输入的时候第二层循环是1~m的。


by PanDaoxi @ 2023-09-02 14:49:02

j <= m


by springisnotcoming @ 2023-09-02 14:50:34

谢谢,已关注!


|