4WAs

B3625 迷宫寻路

yuanzongzi799 @ 2024-11-07 17:46:38

代码:

#include<bits/stdc++.h>
using namespace std;
int n,m;
bool a[51][51];
int dx[]={1,-1,0,0},dy[]={0,0,1,-1},x=1,y=1,q[3][2501];
int bfs()
{

    int head=1,tail=1;
    q[1][1]=x;
    q[2][1]=y;
    while(head<=tail)
    {
        for(int i=0;i<4;i++)
        {
            x=q[1][head]+dx[i];
            y=q[2][head]+dy[i];
            if(a[x][y]==1)
            {
                tail++;
                q[1][tail]=x;
                q[2][tail]=y;
                a[x][y]=0;
                if(x==n&&y==m)
                    return 1;
            }
        }
        head++;
    }
    return 0;
}
int main()
{
    cin>>n>>m;
    char c;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>c;
            if(c=='.')
                a[i][j]=1;
        }
    }
    if(bfs())
        cout<<"Yes";
    else
        cout<<"No";
    return 0;
}

by Dannylxy2012 @ 2024-11-07 17:56:08

@ yuanzongzi799

#include<bits/stdc++.h>
using namespace std;
int minn,n,m,b[105][105];
char a[105][105];
int fx[4]={1,0,-1,0};
int fy[4]={0,1,0,-1};
void dfs(int x,int y,int d){
    b[x][y]=d;
    int tx,ty;
    for(int i=0;i<4;i++){
        tx=x+fx[i];
        ty=y+fy[i];
        if(a[tx][ty]=='.'&&b[tx][ty]>d+1){
            dfs(tx,ty,d+1);
        }
    }
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>a[i][j];
            b[i][j]=INT_MAX;
        }
    }
    dfs(1,1,1);
    if(b[n][m]!=INT_MAX){
        cout<<"Yes";
        return 0;
    }
    cout<<"No";
    return 0;
}

by yuanzongzi799 @ 2024-11-07 18:25:49

@Dannylxy2012 能解释一下不


|