70分,求助!

B3625 迷宫寻路

zet365 @ 2024-02-20 21:09:52

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

int n,m;
char a[102][102];
bool b[102][102],ok = false;

inline void dfs(int x,int y)
{
    b[x][y] = true;
    if(x == n && y == m)
    {
        printf("Yes");
        ok = true;
        return; 
    }
    else
    {
        if(a[x+1][y] != '#' && x + 1 <= n && !b[x+1][y]) dfs(x+1,y);
        if(a[x-1][y] != '#' && x - 1 > 0 && !b[x-1][y]) dfs(x-1,y);
        if(a[x][y+1] != '#' && y + 1 <= m && !b[x][y+1]) dfs(x,y+1);
        if(a[x][y-1] != '#' && y - 1 > 0 && !b[x][y+1]) dfs(x,y-1);
    }
}

int main()
{ 
    memset(b,false,sizeof(b));
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            scanf("%c",&a[i][j]);
        }
    }
    dfs(1,1);
    if(!ok) printf("No");

}

by zyhe2013 @ 2024-02-20 21:16:54


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

int n,m,a1,b1,a2,b2,step[105][105];
char a[105][105];
bool f[105][105];
int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};
struct XY
{
    int x,y;
};
void bfs()
{
    queue<XY>s;
    s.push({1,1});
    f[1][1]=true;
    while(!s.empty())
    {
        int tx=s.front().x,ty=s.front().y;
        s.pop();
        if(tx==n&&ty==m)
        {
            cout<<"Yes"<<"\n";
            return;
        }
        for(int i=0;i<4;i++)
        {
            int nx=tx+dx[i],ny=ty+dy[i];
            if(nx<1||nx>n||ny<1||ny>m)continue;
            if(a[nx][ny]=='#'||f[nx][ny])continue;
            f[nx][ny]++;
            s.push({nx,ny});
            step[nx][ny]=step[tx][ty]+1;
        }
    }
    cout<<"No";
    return;
}

signed main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>a[i][j];
        }
    }
    bfs();
    return 0;
}
```求关

by zyhe2013 @ 2024-02-20 21:17:55

先看,不懂私信问我


by AC_love @ 2024-02-20 21:21:19

scanf 读入字符需要在 %c 前面加个空格 @mating18132617141


by AC_love @ 2024-02-20 21:21:47

scanf(" %c",&a[i][j]);

by AC_love @ 2024-02-20 21:22:17

你的代码一点问题都没,不用改

@mating18132617141


by zet365 @ 2024-02-21 14:25:44

谢谢,已关


|