悬关!感觉逻辑对的,但是样例过不了!不知道为什么debug发现走不到最后一个点
  • 板块B3625 迷宫寻路
  • 楼主coool
  • 当前回复7
  • 已保存回复7
  • 发布时间2023/8/30 12:13
  • 上次更新2023/11/3 00:22:20
查看原帖
悬关!感觉逻辑对的,但是样例过不了!不知道为什么debug发现走不到最后一个点
526922
coool楼主2023/8/30 12:13
#include <bits/stdc++.h>
using namespace std;

int n, m;
char c[105][105];
int vis[105][105];
int direc[4][2] = {{0, -1},{-1, 0}, {0,1}, {1, 0}};

bool check(int x, int y)
{
    if (x < 1 || y < 1 || x > n || y > m || vis[x][y] || c[x][y] == '#')
        return false;
    return true;
}

bool dfs(int x, int y)
{
    if (x == n && y == m)
        return true;
    if (c[n][m] == '#')
        return false;

    for (int i = 0; i < 4; i++)
    {
        int xx = x + direc[i][0];
        int yy = y + direc[i][1];

        if (check(xx, yy))
        {
            cout << x << " " << y << endl;
            vis[xx][yy] = 1;
            dfs(xx, yy);
            vis[xx][yy] = 0;
            
        }
    }

    return false;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> c[i][j];
        }
    }
    vis[1][1] = 1;
    if (dfs(1, 1))
    {
        cout << "Yes" << endl;
    }
    else
        cout << "No" << endl;
    return 0;
}
2023/8/30 12:13
加载中...