C++14 AC C++20全RE

B3625 迷宫寻路

icaijy @ 2024-06-30 00:13:08

如题

#include <bits/stdc++.h>
#define int long long
using namespace std;
int dx[]={0,-1,1,0};
int dy[]={-1,0,0,1};
char a[105][105];
bool vis[105][105];
int n,m;
bool dfs(int i,int j){
    if (!i||!j||i>n||j>m||a[i][j]=='#'||vis[i][j]) return false;
    if (i==n&&j==m) return true;
    vis[i][j]=1;
    for (int k=0;k<4;k++){
        if (dfs(i+dx[k],j+dy[k])) return true;
    }
}
signed main(){
    cin>>n>>m;
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++) cin>>a[i][j];
    }
    if (dfs(1,1)) cout<<"Yes";
    else cout<<"No";

    return 0;
}

by lzm0107 @ 2024-06-30 00:33:42

@icaijy

#include <bits/stdc++.h>
#define int long long
using namespace std;
int dx[]={0,-1,1,0};
int dy[]={-1,0,0,1};
char a[105][105];
bool vis[105][105];
int n,m;
bool dfs(int i,int j){
    if (!i||!j||i>n||j>m||a[i][j]=='#'||vis[i][j]) return false;
    if (i==n&&j==m) return true;
    vis[i][j]=1;
    for (int k=0;k<4;k++){
        if (dfs(i+dx[k],j+dy[k])) return true;
    }
    return false;
}
signed main(){
    cin>>n>>m;
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++) cin>>a[i][j];
    }
    if (dfs(1,1)) cout<<"Yes";
    else cout<<"No";

    return 0;
}

by zzk123_ @ 2024-06-30 07:36:51

@icaijy 你dfs没返回值(false),C++20开O2了,C++14没开O2所以能过


|