迷宫问题(dfs)

迷宫问题

迷宫问题:给你一个而且只有一个出口的迷宫,你需要从入口出发,穿过迷宫,走到出口。要求找到最短路径。

#include <iostream>
#include <vector>

using namespace std;

// 给你二维矩阵,零代表可以走,一代表不可以走,从起点到终点,计算一下最短路径
// 思路:DFS

vector<string> path = {};
vector<string> min_path = {};
vector<tuple<int, int, char>> directions = {{0, 1, 'R'}, {1, 0, 'D'}, {0, -1, 'L'}, {-1, 0, 'U'}};
int start_x, start_y, end_x, end_y;
int min_length = INT_MAX;

// 检查坐标是否在矩阵内,是返回true,否则返回false
bool check(int x, int y, int w, int h)
{
    if (x < 0 || x >= w || y < 0 || y >= h)
    {
        return false;
    }
    return true;
}

void printPath()
{
    cout << "最短路径:" << endl;
    for (auto p : min_path)
    {
        cout << p << " ";
    }
}
void dfs(vector<vector<int>> &matrix, int w, int h, int x, int y, int current_length)
{
    // 不能走到边界或者已经访问过
    if (!check(x, y, w, h) || matrix[x][y] == 1)
    {
        return;
    }

    if (x == end_x && y == end_y)
    {
        if (current_length < min_length)
        {
            min_length = current_length;
            min_path = path;
        }
        return;
    }

    matrix[x][y] = 1; // 标记为已访问

    for (auto [dx, dy, direction] : directions)
    {
        int nx = x + dx, ny = y + dy;
        path.push_back(string(1, direction));
        dfs(matrix, w, h, nx, ny, current_length + 1);
        path.pop_back();
    }

    matrix[x][y] = 0; // 回溯,标记为未访问
}

int test01()
{
    int w, h;
    cin >> w >> h;

    // 读入矩阵
    vector<vector<int>> matrix(h, vector<int>(w));

    for (int i = 0; i < h; i++)
    {
        for (int j = 0; j < w; j++)
        {
            cin >> matrix[i][j];
        }
    }
    cin >> start_x >> start_y >> end_x >> end_y;

    // 开始DFS
    dfs(matrix, w, h, start_x, start_y, 0);

    if (min_length != INT_MAX)
    {
        cout << "找到终点" << endl;
        cout << "最短路径长度为:" << min_length << endl;
    }
    else
    {
        cout << "没有找到终点" << endl;
    }

    printPath();

    return 0;
}

详情可查看博客

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个Python版的深度优先搜索算法生成迷宫问题的示例代码: ```python import random def generate_maze(rows, cols): # 创建一个 rows 行 cols 列的二维数组,初始化为全 1 maze = [[1] * cols for _ in range(rows)] # 将起点设置为 (0, 0),并将其标记为 0 start_row, start_col = 0, 0 maze[start_row][start_col] = 0 # 使用深度优先搜索算法生成迷宫 dfs(maze, start_row, start_col) return maze def dfs(maze, row, col): # 定义四个方向:上、下、左、右 directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] # 随机打乱方向的顺序 random.shuffle(directions) # 遍历每个方向 for direction in directions: # 计算新的坐标 new_row, new_col = row + direction[0], col + direction[1] # 判断新坐标是否越界或已经被访问过 if (new_row < 0 or new_row >= len(maze) or new_col < 0 or new_col >= len(maze[0]) or maze[new_row][new_col] == 0): continue # 打通当前坐标与新坐标之间的墙壁 if direction == (0, -1): # 左 maze[row][col-1] = 0 elif direction == (0, 1): # 右 maze[row][col+1] = 0 elif direction == (-1, 0): # 上 maze[row-1][col] = 0 elif direction == (1, 0): # 下 maze[row+1][col] = 0 # 递归访问新坐标 dfs(maze, new_row, new_col) # 生成一个 5x5 的迷宫 maze = generate_maze(5, 5) # 打印迷宫 for row in maze: print(row) ``` 这个示例代码可以生成一个指定大小的迷宫,并使用深度优先搜索算法生成迷宫的路径。其中,生成迷宫的主要逻辑在 `generate_maze()` 函数中,它首先创建一个二维数组表示迷宫,然后使用深度优先搜索算法从起点开始访问迷宫中的所有格子,并打通相邻格子之间的墙壁,最终生成迷宫的路径。在这个示例代码中,我们使用随机打乱方向的顺序来增加迷宫的随机性。最后,我们打印生成的迷宫,查看迷宫的路径和墙壁的布局。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

0zxm

祝大家天天开心

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值