求助大佬!

B3625 迷宫寻路

chenyihao2011 @ 2024-03-16 21:16:37

也不知道该怎么调

#include<bits/stdc++.h>
using namespace std;

int n,m;
char c;
bool cnt=false;
int a[105][105]={0};

int dfs(int x,int y){
    if(x==n and y==m){
        cnt=true;
        return true;
    }
    int d,b;
    int xi[5]={0,1,0,-1,0};
    int yi[5]={0,0,1,0,-1};
    for(int i=1;i<=4;i++){
        d=x+xi[i];
        b=y+yi[i];
        if(d<=n and d>=1 and b<=m and b>=1 and a[x][y]==1) return dfs(d,b);
    }
    return false;
}
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;
        }
    }
    cnt=dfs(1,1);
    if(cnt) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    return 0;
} 

by NorthStar @ 2024-03-17 19:45:04

@chenyihao2011

#include <bits/stdc++.h>
using namespace std;

int n, m;
char c;
bool cnt = false;
int a[105][105] = {0}, vis[105][105];

void dfs(int x, int y) {
    if (x == n and y == m) {
        cout << "Yes";
        exit(0);
    }
    int d, b;
    int xi[5] = {0, 1, 0, -1, 0};
    int yi[5] = {0, 0, 1, 0, -1};
    for (int i = 1; i <= 4; i++) {
        d = x + xi[i];
        b = y + yi[i];
        if (d <= n and d >= 1 and b <= m and b >= 1 and a[d][b] == 1 and vis[d][b] == 0) {
            vis[d][b] = 1;
            dfs(d, b);
        }
    }
}
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;
        }
    }
    dfs(1, 1);
    cout << "No" << endl;
    return 0;
}

by aishiteru_mitsu_ha @ 2024-03-18 17:53:13

#include<bits/stdc++.h>
using namespace std;
char a[505][505];
bool no[505][505],yes;
long long n,m,ans;
int dx[4]{0,1,-1,0},dy[4]{1,0,0,-1};
void dfs(long long x,long long y);
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>a[i][j];
            if(a[i][j]=='.'){
                no[i][j]=true;
            }
        }
    }
    dfs(1,1);
    if(yes==true){
        cout<<"Yes"<<endl;
    }else{
        cout<<"No"<<endl;
    }
    return 0;
}
void dfs(long long x,long long y){
    no[x][y]=false;
    if(x==n&&y==m){
        yes=true;
        return;
    }
    for(int i=0;i<4;i++){
        if(no[x+dx[i]][y+dy[i]]==true){
            dfs(x+dx[i],y+dy[i]);
        }
    }
}

by chenyihao2011 @ 2024-03-20 15:48:52

@Ye_zixiao 谢谢!


by chenyihao2011 @ 2024-03-20 15:49:35

@not_much 谢谢!


|