40分DFS求调谢谢大佬!

B3625 迷宫寻路

Pianmu @ 2023-05-14 20:50:50

#include <bits/stdc++.h>
using namespace std;
const int maxn=105;
bool vis[maxn][maxn];
char mapp[maxn][maxn];
int m,n;
bool ans=false;
int dx[4]={0,1,-1,0};
int dy[4]={1,0,0,-1};
void dfs(int x,int y)
{
    if (x<0||y<0||mapp[x][y]=='#'||vis[x][y]==true||x>m||y>n)
        return ;
    vis[x][y]=true;
    if (x==m-1&&y==n-1)
    {
        ans=true;
        return ;
    }
    for (int i=0;i<4;i++)
    {
        int sx=x+dx[i];
        int sy=y+dy[i];
        if (!vis[sx][sy]&&mapp[sx][sy]=='.'&&sx<=m&&sy<=n)
        {
            vis[sx][sy]=true;
            dfs(sx,sy);
        }
    }
}
int main()
{
    cin>>n>>m;
    for (int i=0;i<n;i++)
    {
        for (int j=0;j<m;j++)
        {
            cin>>mapp[i][j];
        }
    }
    dfs(0,0);
    if (ans)
        cout<<"Yes"<<endl;
    else
        cout<<"No"<<endl;
    return 0;
}

by SealMoBurp @ 2023-06-19 21:41:17

@Pianmu if (!vis[sx][sy]&&mapp[sx][sy]=='.'&&sx<=m&&sy<=n)这行还需要判断sx和sy>=0


by Pianmu @ 2023-07-10 08:23:55

@Seal_ZhouMoTong 谢谢大佬!


|