2501_90427444 2025-06-02 02:26 采纳率: 50%
浏览 8

我用DFS生成迷宫路径不完全还有死路怎么办呀

img


代码运行出来长这个样子,我写了3,4,个小时了,还是有问题,哪位朋友可以帮我看看吗,超级感谢
下面是我的代码


#include <iostream>
#include <cstdlib>
#include<ctime>
#include<algorithm>
using namespace std;
int  map[100][100];
int v[100][100];
//0未访问,1 访问
int dx[4] = { 0,1,0,-1 };
int dy[4] = { 1,0,-1,0 };
//初始化迷宫
void initMaze(int row, int column)
{
    for (int i = 0; i < row + 2; i++)
    {
        for (int j = 0; j < column + 2; j++)
        {
            if (i == 0 || i == row + 1 || j == 0 || j == column + 1)
            {
                map[i][j] = 1;
            }
            else
            {
                map[i][j] = 1;
                v[i][j] = 0;
            }
        }
    }
}
//DFS递归生成迷宫(带自然回溯)
void dfs(int x, int y,int row,int column)
{
    v[x][y] = 1;
    map[x][y] = 0;
    int dirs[4] = { 0,1,2,3 };
    for (int i = 0; i < 4; i++)
    {
        int r = rand() % 4;
        swap(dirs[i], dirs[r]);
    }
    for (int i = 0; i < 4; i++)
    {
        //int k = rand() % 4;
        int dir = dirs[i];
        int nx = x + dx[dir] * 2;
        int ny = y + dy[dir] * 2;
        if (nx > 0 && nx <= row && ny > 0 && ny <= column && v[nx][ny] == 0)
        {
            map[x + dx[dir]][y + dy[dir]] = 0;
            dfs(nx, ny, row, column);
        }
    }
}
//打印迷宫
void printmaze(int row,int column)
{
    for (int i = 0; i < row + 2; i++)
    {
        for (int j = 0; j < column + 2; j++)
        {
            if (i ==1 && j == 0)
            {
                cout << "S";
            }
            else if (i == row && j == column + 1)
            {
                cout << "E";
            }
            else if (map[i][j] == 1)
                cout << "■";
            else
            {
                cout << " ";
            }
        }
        cout << endl;
    }
}


int main()
{
    srand(time(0));
    cout << "请输入迷宫的行数(<98)" << endl;
    int row;
    cin >> row;
    cout << "请输入迷宫的列数(<98)" << endl;
    int column;
    cin >> column;
    initMaze(row,column);
    dfs(1, 1, row, column);
    printmaze(row, column);

    return 0;
}
  • 写回答

6条回答 默认 最新

  • 阿里嘎多学长 2025-06-02 02:26
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    解决方案

    你的问题是使用 DFS 生成迷宫路径,但是生成的路径不完整,还有死路。这可能是因为你的 DFS 算法没有正确地探索迷宫的所有可能路径。

    以下是一些可能的解决方案:

    1. 检查 DFS 算法的实现:确保你的 DFS 算法正确地探索迷宫的所有可能路径。可以通过打印出每个节点的访问顺序来检查。
    2. 使用 BFS 算法:如果 DFS 算法不能正确地生成迷宫路径,可以尝试使用 BFS 算法。BFS 算法可以更好地探索迷宫的所有可能路径。
    3. 添加路径检查:在生成迷宫路径时,可以添加路径检查机制,检查当前路径是否已经访问过。如果已经访问过,可以跳过该路径。
    4. 使用优化算法:可以使用优化算法,例如 A* 算法或 Dijkstra 算法,来生成迷宫路径。这些算法可以更好地探索迷宫的所有可能路径。

    以下是一个简单的 DFS 算法示例代码:

    void dfs(int x, int y, vector<vector<int>>& maze, vector<vector<bool>>& visited, vector<pair<int, int>>& path) {
        if (x < 0 || x >= maze.size() || y < 0 || y >= maze[0].size() || visited[x][y]) {
            return;
        }
        visited[x][y] = true;
        path.push_back({x, y});
        if (maze[x][y] == 0) {
            // 遇到墙壁,返回
            return;
        }
        // 探索四个方向
        dfs(x - 1, y, maze, visited, path);
        dfs(x + 1, y, maze, visited, path);
        dfs(x, y - 1, maze, visited, path);
        dfs(x, y + 1, maze, visited, path);
    }
    

    这个算法使用 DFS 探索迷宫的所有可能路径,并将路径存储在 path 向量中。

    希望这些解决方案能够帮助你解决问题。如果你需要更多帮助,请提供更多代码和详细信息。

    评论

报告相同问题?

问题事件

  • 创建了问题 6月2日