救命

B3625 迷宫寻路

mayiwen @ 2024-08-15 17:30:59

#include<bits/stdc++.h>
using namespace std;
char a[110][110];
int xx[11000],yy[11000],u[4]={1,-1,0,0},v[4]={0,0,1,-1};
int n,m,s=0;
void bfs(int x,int y)
{
    int t=1,w=0;
    xx[1]=x;
    yy[1]=y;
    while(t>=w)
    {
        w++;
        for(int i=0;i<=3;i++)
        {
            int q=xx[w]+u[i],p=yy[w]+v[i];
            if(q<=n&&q>=1&&p<=m&&p>=1&&a[q][p]=='.')
            {
                if(q==n&&p==m)
                {
                    s=1;
                    return;
                }
                t++;
                xx[t]=x;
                yy[t]=y;
            }
        }
    }
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            cin>>a[i][j];
    bfs(1,1);
    if(n==1&&m==1)
    {
        cout<<"Yes";
        return 0;
    }
    if(s==0)
        cout<<"No";
    else
        cout<<"Yes";
    return 0;
}

by mayiwen @ 2024-08-15 17:31:18

40


by ysmocc @ 2024-08-15 17:32:09

nb


by lxr_Galaxy @ 2024-08-15 17:35:01

#include<bits/stdc++.h>
using namespace std;
int n,m,k=0,flag=0;
char a[103][103];
int vis[103][103];
int cz(int x,int y){
    if(x==n && y==m){
        return 1;
    }
    if(x>n || y>m || x<1 || y<1 || a[x][y]=='#' || vis[x][y]==1) return 0;
    vis[x][y]=1;
    if(cz(x,y+1))return 1;
    if(cz(x+1,y))return 1;
    if(cz(x,y-1))return 1;
    if(cz(x-1,y))return 1;
    return 0;

}
int main(){

    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>a[i][j];
        }
    }
    if(cz(1,1)) cout<<"Yes";
    else cout<<"No"; 
} 

@mayiwen 有点难改,你先参考下我的


by yuki_lyq @ 2024-08-16 18:35:02

说实话,深搜的代码也是只能理解自己的,别人的代码就很懵逼


|