dfs70分求助

B3625 迷宫寻路

Xiaoshi_ @ 2023-09-28 19:22:34

求给位大佬帮我看看qwq


#include<istream>
#include<ostream>
#include<stdlib.h>
namespace std _GLIBCXX_VISIBILITY(default){
  extern istream cin;
  extern ostream cout;
  static ios_base::Init __ioinit;
}
using std::cin;
using std::cout;
using std::exit;
bool map[120][120];
int n,m;
int dx[4]={1,0,0,-1};
int dy[4]={0,-1,1,0};
void dfs(int x,int y){
    if(x==n && y==m){
        cout<<"Yes";
        exit(0);
    }
    bool flag=1;
    for(int i=0;i<4;i++){
        int ix=x+dx[i],iy=y+dy[i];
        if(map[ix][iy]){
            flag=0;
            map[x][y]=0;
            dfs(ix,iy);
            map[x][y]=1;
        }else{
            continue;
        }
    }
    if(flag)
        return;
}
int main(){
    std::ios::sync_with_stdio(false);
    char t;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>t;
            map[i][j]=(t-'#');
        }
    }
    dfs(1,1);
    cout<<"No";
}

by liu_le_chen @ 2023-09-28 19:27:13

@shijunyu2010 我个人喜欢用BFS,您想要看看代码吗?


by Xiaoshi_ @ 2023-09-28 19:27:56

@liu_le_chen 行


by liu_le_chen @ 2023-09-28 19:29:35

@shijunyu2010 希望您能为我送上一个不要钱的关注(学抖音的)

#include<bits/stdc++.h>
using namespace std;
long long n,m,f,flag[1001][1001],vis[4][2]={-1,0,1,0,0,-1,0,1};
char arr[1001][1001];
struct node{
    int x,y;
};
queue<node> q;
void bfs(){
    q.push({1,1});
    flag[1][1]=1;
    while(!q.empty()){
        for(int i=0;i<4;i++){
            int xx=q.front().x+vis[i][0];
            int yy=q.front().y+vis[i][1];
            if(arr[xx][yy]=='.' && flag[xx][yy]==0 && xx>=1 && xx<=n && yy>=1 && yy<=m){
                flag[xx][yy]=1;
                if(xx==n && yy==m){
                    cout<<"Yes";
                    f=0;
                    return;
                }
                q.push({xx,yy});
            }
        }
        q.pop();
    }
}
int main(){
    f=1;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>arr[i][j];
        }
    }
    bfs();
    if(f) cout<<"No";
    return 0;
}

by wzhm54nr @ 2023-09-28 20:14:45

我改了一下,有注释

#include<bits/stdc++.h>
using namespace std;
bool mp[120][120];//个人习惯,万能头
int n,m;
int dx[4]={1,0,0,-1};
int dy[4]={0,-1,1,0};
void dfs(int x,int y){
    if(x==n && y==m){
        cout<<"Yes";
        exit(0);
    }
    for(int i=0;i<4;i++){
        int ix=x+dx[i],iy=y+dy[i];
        if(mp[ix][iy]){
            mp[x][y]=0;
            dfs(ix,iy);
                        //mp[x][y]=1;//无需再次赋为1
        }
    }
}
int main(){
    char t;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>t;
            if(t=='#')  mp[i][j]=0;
            else  mp[i][j]=1;
                        //我的读入方式更加安全,大考建议使用
        }
    }
    dfs(1,1);
    cout<<"No";
}

by craftmine @ 2023-09-30 11:57:12

me的也see下

#include<bits/stdc++.h>
using namespace std;
int mp[110][110],n,m;
bool vis[110][110];
int dx[]={-1,1,0,0},dy[]={0,0,1,-1};
void dfs(int x,int y){
    if(!vis[x][y]){
        return;
    }
    if(x==n&&y==m){
        cout<<"Yes";
        exit(0);
    }
    vis[x][y]=0; 
    for(int i=0;i<4;i++){
        int xx=x+dx[i],yy=y+dy[i];
        if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&vis[xx][yy]){
            dfs(xx,yy);
        }
    }
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        string s;
        cin>>s;
        for(int j=1;j<=m;j++){
            mp[i][j]=s[j-1];
            if(mp[i][j]=='.'){
                vis[i][j]=true;
            }
            else{
                vis[i][j]=false; 
            }
        }
    }
    dfs(1,1);
    cout<<"No";
    return 0;
}

by Xiaoshi_ @ 2023-10-07 00:45:02

@FUchenhao123 @ wzhm54nr 谢谢你们,已经懂了


|