MLE 0分求调

B3625 迷宫寻路

linjitong @ 2024-05-06 19:23:02

测试点:点我

code:

#include <bits/stdc++.h>
using namespace std;
int x[] = {0, 0, 0, 1, -1}, y[] = {0, 1, -1, 0, 0};
char a[110][110];
int n,m;
int flag=0;
void dfs(int x1, int y1)
{
    for (int i = 1; i <= 4; i++)
    {
        int x2 = x1 + x[i], y2 = y1 + y[i];
        if (x2 >= 1 || x2 <= n || y2 >= 1 || y2 <= m)
        {
            if(x2==n && y2==m)
            {
                flag=1;
                return;
            }
            else if(x2>=1&&x2<=n&&y2>=1&&y2<=m&&a[x2][y2]=='.')
            {
                dfs(x2, y2);
            }
        }
    }
}
int main()
{
    int x1, y1;
    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(flag)
        cout<<"Yes"<<endl;
    else
        cout<<"No"<<endl;
    return 0;
}

by linjitong @ 2024-05-06 19:27:42

最新code:点我


by sutiancheng @ 2024-05-06 19:52:13

#include<iostream>
using namespace std;
int n,m;
char a[105][105];
bool b[105][105];
void dfs(int x,int y)
{
    if(b[x][y])return;
    if(a[x][y]=='#')return;
    b[x][y]=1;
    if(x>1)dfs(x-1,y);
    if(x<n)dfs(x+1,y);
    if(y>1)dfs(x,y-1);
    if(y<m)dfs(x,y+1);
    return;
}
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(b[n][m])cout<<"Yes";
    else cout<<"No";
    return 0;
}

这是我的代码


by Lijunzhuo @ 2024-05-06 19:55:05

你不判重复路径啊?


by Lijunzhuo @ 2024-05-06 19:55:16

@linjitong


by Lijunzhuo @ 2024-05-06 19:55:56

 if (x2 >= 1 || x2 <= n || y2 >= 1 || y2 <= m)

这个用或啊? @linjitong


by Lijunzhuo @ 2024-05-06 19:58:28

@linjitong @linjitong @linjitong @linjitong @linjitong @linjitong


by linjitong @ 2024-05-06 20:26:47

已解决 ???


by Lijunzhuo @ 2024-05-06 20:29:10


by rensiwei @ 2024-08-21 14:53:04

这是在聊天???


|