60pts,悬关

B3625 迷宫寻路

Very_Smart_Honet @ 2023-09-24 14:35:19

求给位大佬帮我看看(悬关)

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m;
char s[110][110];
int flag;
int dr[4]={-1,0,1,0};
int dc[4]={0,1,0,-1};
int vis[110][110];
int cnt;
bool check(int x,int y)
{
    if(x<1||x>n||y<1||y>m||vis[x][y]==1||s[x][y]=='#')
    {
        return false;
    }
    return true;
}
void dfs(int row,int col)
{
    if(s[row][col]=='#') return;
    if(row==n&&col==m) flag=1;
    if(flag==1) return;
    for(int i=0;i<4;i++)
    {
        int r=row+dr[i];
        int c=col+dc[i];
        if(check(r,c)==true) 
        {
            vis[r][c]=1;
            dfs(r,c);
            vis[r][c]=0;
        }
    }
    return;
}
signed main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>s[i][j];
    vis[1][1]=1;
    dfs(1,1);
    if(flag) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    return 0;
}

by xQWQx @ 2023-09-24 15:05:56

@wangzihang2012

#include<bits/stdc++.h>
using namespace std;
int n,m;
char s[110][110];
int flag;
bool check(int x,int y)
{
    if(x<1||x>n||y<1||y>m||s[x][y]=='#')
    {
        return false;
    }
    return true;
}
void dfs(int row,int col)
{
    if(check(row,col)==false) return ; 
    if(row==n&&col==m){
        cout<<"Yes";
        exit(0);
    }
    s[row][col]='#';
    dfs(row-1,col);
    dfs(row,col+1);
    dfs(row+1,col);
    dfs(row,col-1);
    return;
}
signed main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>s[i][j];
    dfs(1,1);
    cout<<"No"<<endl;
    return 0;
}

改好了


by Very_Smart_Honet @ 2023-09-24 15:21:33

已关,我的问题出在哪了?


by Very_Smart_Honet @ 2023-09-24 15:23:53

我刚刚又改了一遍,您看一下

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,m;
char s[110][110];
int flag;
int dr[4]={-1,0,1,0};
int dc[4]={0,1,0,-1};
int vis[110][110];
int cnt;
void dfs(int row,int col)
{
    if(row==n&&col==m)
    {
        flag=1;
        return;
    }
    vis[row][col]=1;
    for(int i=0;i<4;i++)
    {
        int r=row+dr[i];
        int c=col+dc[i];
        if(r>=1&&r<=n&&c>=1&&c<=m&&s[r][c]=='.')
        {
            if(vis[r][c]==0) dfs(r,c);
        }
    }
}
signed main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>s[i][j];
    vis[1][1]=1;
    if(s[1][1]=='#'||s[n][m]=='#') 
    {
        cout<<"No"<<endl;
        return 0;
    }
    dfs(1,1);
    if(flag) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    return 0;
}

by xQWQx @ 2023-09-24 15:45:30

@wangzihang2012

vis[r][c]=0;

这里不需要解锁


by xQWQx @ 2023-09-24 15:52:32

@wangzihang2012

刚开始没有看见,所以改动较大,其实把这一行去掉就行了。⊙﹏⊙∥

你下次回复记得@


by Very_Smart_Honet @ 2023-09-24 22:22:05

感谢


|