迷宫问题 | 深度优先

目录

一、说明

二、步骤

三、代码

四、结果


一、说明

什么是深度优先?

        DFS即Depth First Search,深度优先搜索属于图算法的一种,是一个针对图和树的遍历算法,利用深度优先搜索算法可以产生目标图的相应拓扑排序表,一般用堆数据结构来辅助实现DFS算法。其过程简要来说是对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次。

本文通过对10*10迷宫问题的解决,理解深度优先的代码逻辑策略。

操作系统:win 10

编辑器:pycharm edu

语言及版本:python 3.10

解决的问题:迷宫

使用的算法:深度优先

迷宫复杂度:10*10

替代迷宫:列表(1代表墙壁,0代表通道,即1不能走,0可以走)

存储容器:栈(python中的列表可实现栈的功能,先进后出)

可走出迷宫路线数量:2

迷宫图:

[

[0, 0, 1, 1, 1, 1, 1, 1, 1, 1],

[0, 0, 1, 1, 0, 0, 0, 0, 1, 1],

[1, 0, 0, 0, 0, 1, 1, 0, 1, 1],

[1, 1, 1, 0, 0, 1, 1, 0, 1, 1],

[1, 1, 1, 0, 0, 1, 1, 0, 0, 1],

[1, 1, 1, 1, 0, 0, 1, 0, 0, 1],

[1, 1, 1, 1, 1, 0, 0, 1, 0, 1],

[1, 1, 1, 1, 1, 1, 0, 0, 0, 1],

[1, 1, 1, 1, 1, 1, 0, 1, 0, 1],

[1, 1, 1, 1, 1, 1, 0, 0, 0, 0],

]

逻辑:

先确定当前节点,在判断当前节点上下左右是否是墙壁(要去除掉之前走过的节点,避免走死循环),往上下左右的优先级看实际情况,不是墙壁即把这个点入栈,并把这个当做当前节点,循环反复,直到当前点坐标等于终点坐标即为走出迷宫;

如何判断迷宫是否可以走出?

1)若迷宫可以走出,则当前节点坐标等于终点坐标;

2)若迷宫无法走出,则记录坐标的容器为空;

注意:当走到某一条死路进行后退时,要把回退的节点出栈,直到下一条路线为止;

二、步骤

2.1、创建迷宫并确认起始点和终点

    # 创建迷宫
    maze_pic = [
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 0, 1, 1, 0, 0, 0, 0, 1, 1],
        [1, 0, 0, 0, 0, 1, 1, 0, 1, 1],
        [1, 1, 1, 0, 0, 1, 1, 0, 1, 1],
        [1, 1, 1, 0, 0, 1, 1, 0, 0, 1],
        [1, 1, 1, 1, 0, 0, 1, 0, 0, 1],
        [1, 1, 1, 1, 1, 0, 0, 1, 0, 1],
        [1, 1, 1, 1, 1, 1, 0, 0, 0, 1],
        [1, 1, 1, 1, 1, 1, 0, 0, 0, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    ]
    # 确定起点
    stat = (0, 0)
    # 确定终点,索引值从0开始,0-9=10
    stop = (8, 8)

2.2、取出当前节点并记录

        now = lst[-1]
        row, col = now  # 取出当前节点
        # 9表示当前节点已走过,设置什么数字或者字母无所谓,只要不是0即可
        maze_pic[row][col] = 9  

2.3、走出迷宫的条件并判断各个方向是否能走并记录节点

        if now == stop:
            print("迷宫已走出,走过的路线为:\n", lst)
            break
        if maze_pic[row-1][col] == 0:  # 上
            lst.append((row-1, col))  # 记录节点
            continue
        elif maze_pic[row+1][col] == 0:  # 下
            lst.append((row+1, col))
            continue
        elif maze_pic[row][col-1] == 0:  # 左
            lst.append((row, col-1))
            continue
        elif maze_pic[row][col+1] == 0:  # 右
            lst.append((row, col+1))
            continue
        else:
            lst.pop()

三、代码

完整代码如下:



def maze_question():
    # 创建迷宫
    maze_pic = [
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 0, 1, 1, 0, 0, 0, 0, 1, 1],
        [1, 0, 0, 0, 0, 1, 1, 0, 1, 1],
        [1, 1, 1, 0, 0, 1, 1, 0, 1, 1],
        [1, 1, 1, 0, 0, 1, 1, 0, 0, 1],
        [1, 1, 1, 1, 0, 0, 1, 0, 0, 1],
        [1, 1, 1, 1, 1, 0, 0, 1, 0, 1],
        [1, 1, 1, 1, 1, 1, 0, 0, 0, 1],
        [1, 1, 1, 1, 1, 1, 0, 0, 0, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
    ]
    # 确定起点
    start = (1, 1)
    # 确定终点,索引值从0开始,0-9=10
    stop = (8, 8)
    # 判断逻辑
    lst = [start]
    # 走过的节点不为空时才继续判断下一步
    i = 0  # 计数
    while lst:
        i += 1
        now = lst[-1]
        row, col = now  # 取出当前节点
        # 9表示当前节点已走过,设置什么数字或者字母无所谓,只要不是0即可
        maze_pic[row][col] = 9
        if now == stop:
            print("走过节点数为:", i)
            print("迷宫已走出,走过的路线为:\n", lst)
            break
        if maze_pic[row-1][col] == 0:  # 上
            lst.append((row-1, col))
            continue
        elif maze_pic[row+1][col] == 0:  # 下
            lst.append((row+1, col))
            continue
        elif maze_pic[row][col-1] == 0:  # 左
            lst.append((row, col-1))
            continue
        elif maze_pic[row][col+1] == 0:  # 右
            lst.append((row, col+1))
            continue
        else:
            lst.pop()
    else:
        print("该迷宫无法走出")
        print("走过的节点个数为:", i)


if __name__ == '__main__':
    maze_question()

四、结果

4.1、代码运行结果

结果较长,只截取了部分,如下图1:

图1

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用Java深度优先算法解决迷宫问题的示例代码: ```java import java.util.Stack; public class MazeSolver { private int[][] maze; private int[][] solution; private int rows; private int cols; private int startRow; private int startCol; private int endRow; private int endCol; public MazeSolver(int[][] maze) { this.maze = maze; this.rows = maze.length; this.cols = maze[0].length; this.solution = new int[rows][cols]; this.startRow = 0; this.startCol = 0; this.endRow = rows - 1; this.endCol = cols - 1; } public boolean solveMaze() { Stack<Cell> stack = new Stack<>(); Cell startCell = new Cell(startRow, startCol); stack.push(startCell); while (!stack.isEmpty()) { Cell currentCell = stack.pop(); int row = currentCell.getRow(); int col = currentCell.getCol(); if (row < 0 || row >= rows || col < 0 || col >= cols) { continue; } if (maze[row][col] == 0 && solution[row][col] == 0) { solution[row][col] = 1; if (row == endRow && col == endCol) { return true; } stack.push(new Cell(row + 1, col)); stack.push(new Cell(row - 1, col)); stack.push(new Cell(row, col + 1)); stack.push(new Cell(row, col - 1)); } } return false; } public void printSolution() { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { System.out.print(solution[i][j] + " "); } System.out.println(); } } private class Cell { private int row; private int col; public Cell(int row, int col) { this.row = row; this.col = col; } public int getRow() { return row; } public int getCol() { return col; } } } // 示例用法 int[][] maze = { {1, 1, 1, 1, 1}, {1, 0, 0, 0, 1}, {1, 1, 1, 0, 1}, {1, 0, 0, 0, 1}, {1, 1, 1, 1, 1} }; MazeSolver solver = new MazeSolver(maze); boolean isSolvable = solver.solveMaze(); if (isSolvable) { System.out.println("The maze is solvable."); solver.printSolution(); } else { System.out.println("The maze is not solvable."); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值