90pts求调

B3625 迷宫寻路

blankslpl @ 2024-10-12 17:22:02

rt玄关

#include<bits/stdc++.h>
using namespace std;
const int N=110;
char a[N][N];
int dx[]={0,1,0,-1},n,m;
int dy[]={1,0,-1,0};
bool s[N][N],o=0;
void dfs(int x,int y)
{
    if(x==n&&y==m)
    {
        o=1;
        return;
    }
    s[x][y]=1;
    bool f=1;
    for(int i=0;i<4;i++)
    {
        int xx=x+dx[i],yy=y+dy[i];
        if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&!s[xx][yy]&&a[xx][yy]!='#')
        {
            if(xx==n&&yy==n)
            {
                o=1;
                return;
            }
            s[xx][yy]=1;
            dfs(xx,yy);
            f=0;
        }
    }
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>a[i][j];
        }
    }
    dfs(1,1);
    if(o) cout<<"Yes";
    else cout<<"No";
    return 0;
}

by hepp @ 2024-10-12 17:25:39

#include<bits/stdc++.h>
using namespace std;
const int N=110;
char a[N][N];
int dx[]={0,1,0,-1},n,m;
int dy[]={1,0,-1,0};
bool s[N][N],o=0;
void dfs(int x,int y)
{
    if(x==n&&y==m)
    {
        o=1;
        return;
    }
    s[x][y]=1;
    bool f=1;
    for(int i=0;i<4;i++)
    {
        int xx=x+dx[i],yy=y+dy[i];
        if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&!s[xx][yy]&&a[xx][yy]!='#')
        {
            if(xx==n&&yy==m)//改成m
            {
                o=1;
                return;
            }
            s[xx][yy]=1;
            dfs(xx,yy);
            f=0;
        }
    }
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>a[i][j];
        }
    }
    dfs(1,1);
    if(o) cout<<"Yes";
    else cout<<"No";
    return 0;
}

by hepp @ 2024-10-12 17:25:51

@blankslpl


by kn01 @ 2024-10-12 17:40:53

@blankslpl

if(xx==n&&yy==n)

改为

if(xx==n&&yy==m)

by blankslpl @ 2024-10-12 21:24:38

@kn01 谢谢已关


by blankslpl @ 2024-10-12 21:25:24

@hepp 谢谢已回光


|