求大佬帮助
查看原帖
求大佬帮助
797897
_QyGyQ_楼主2023/7/28 11:14
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+7;
using ll=long long;
int n,m,ex,ey,step;
char a[200][200];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
void dfs(int x,int y){
    if(x==ex&&y==ey){
        step=1;
        return ;
    }
    else{
        a[x][y]='#';
        for(int i=0;i<4;i++){
            int tx=x+dx[i];
            int ty=y+dy[i];
            if(a[tx][ty]=='.'){
                dfs(tx,ty);
            }
        }
    }
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>a[i][j];
        }
    }
    ex=n,ey=m;
    dfs(1,1);
    if(step==1) cout<<"yes";
    else cout<<"no";
    return 0;
}

这一篇全WA,感觉不用回溯。但听了我发的上一篇帖的大佬说要回溯,所以就变成

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+7;
using ll=long long;
int n,m,ex,ey,step;
char a[200][200];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
void dfs(int x,int y){
    if(x==ex&&y==ey){
        step=1;
        return ;
    }
    else{
        a[x][y]='#';
        for(int i=0;i<4;i++){
            int tx=x+dx[i];
            int ty=y+dy[i];
            if(a[tx][ty]=='.'){
                dfs(tx,ty);
                a[x][y]='.';
            }
        }
    }
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>a[i][j];
        }
    }
    ex=n,ey=m;
    dfs(1,1);
    if(step==1) cout<<"yes";
    else cout<<"no";
    return 0;
}

就WA,TLE,MLE了。求助!!!(无奈)

2023/7/28 11:14
加载中...