60pts求助!

B3625 迷宫寻路

E_RONALDO @ 2024-03-26 20:13:05

#include<bits/stdc++.h>
using namespace std;
int n,m,a[106][106];
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
char c;
bool flag=false;
void dfs(int x,int y){
    if(x==n&&y==n){
        flag=true;
        return;
    }
    a[x][y]=1;
    for(int i=0;i<4;i++){
        int nx=x+dx[i];
        int ny=y+dy[i];
        if(nx>m||ny>n||nx<1||ny<1||a[nx][ny]==1){
            continue;
        } 
        dfs(nx,ny);
    }
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>c;
            if(c=='#'){
                a[i][j]=1;
            }
            else{
                a[i][j]=0;
            } 
        }
    }
    dfs(1,1);
    if(!flag){
        cout<<"No";
    }
    else{
        cout<<"Yes";
    }

    return 0;
} 

by XiaoC111 @ 2024-03-29 18:32:40

都是小错误,if(x==n&&y==n) if(nx>m||ny>n||nx<1||ny<1||a[nx][ny]==1) x对应n,y对应m


|