if you WA on #2#3#4#5#7#9

B3625 迷宫寻路

wky_wsy @ 2024-11-11 22:34:56

#include <iostream>
#include <string>
#include <algorithm>
#define int long long
using namespace std;
char a[110][110];
int n,m;
void dfs(int x,int y){
    if(x==n&&y==m){
        printf("Yes");
        exit(0);
    }
    if(x>=1&&x<=n&&y>=1&&y<=m&&a[x][y]=='.'){
        dfs(x,y+1);
        dfs(x+1,y);
        dfs(x,y-1);
        dfs(x-1,y);
    }
}
signed main(){
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            scanf("%c",&a[i][j]);
        }
    }
    dfs(1,1);
    printf("No");
    return 0;
}

by jiangyixuan_eason @ 2024-11-12 19:31:25

@wky_wsy

#include <iostream>
using namespace std;
int n,m,a[200][200];
int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
bool f = false;
char l;
void dfs(int x,int y){
    if(x == n && y == m){
        f = true;
        return;
    }
    a[x][y] = 1;
    for(int i = 0;i < 4;++i){
        int x2 = x + dx[i],y2 = y + dy[i];
        if(x2 > 0 && x2 <= n && y2 > 0 && y2 <= m){
            if(a[x2][y2] == 0){
                dfs(x2,y2);
            }
        }
    }
}
int main(){
    cin >> n >> m;
    for(int i = 1;i <= n;++i){
        for(int j = 1;j <= m;++j){
            cin >> l;
            if(l == '#'){
                a[i][j] = 1;
            }
        }
    }
    if(a[1][1] == 1 || a[n][m] == 1){
        cout << "No";
        return 0;
    }
    dfs(1,1);
    if(f == true){
        cout << "Yes";
    }else{
        cout << "No";
    }

    return 0;
}

|