Maze problem | Depth first

Table of contents

1. Description

2. Steps

3. Code

4. Results


1. Description

What is depth first?

        DFS is Depth First Search. Depth-first search is a type of graph algorithm. It is a traversal algorithm for graphs and trees. The depth-first search algorithm can be used to generate the corresponding topological sorting table of the target graph. Heap data structures are generally used to assist in the implementation of DFS. algorithm. Briefly speaking, the process is to drill down into every possible branch path until it cannot go any deeper, and each node can only be visited once.

This article understands the depth-first code logic strategy by solving the 10*10 maze problem.

Operating system: win 10

Editor:pycharmedu

Language and version: python 3.10

Problem solved: maze

Algorithm used: depth first

Maze complexity: 10*10

Alternative maze: list (1 represents the wall, 0 represents the passage, that is, 1 cannot be walked, 0 can be walked)

Storage container: stack (a list in python can implement the function of a stack, first in, last out)

Number of routes to exit the maze: 2

Maze diagram:

[

[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],

]

logic:

First determine the current node, and then determine whether the top, bottom, left, and right of the current node are walls (remove the nodes that have been passed before to avoid an endless loop), and look at the actual situation by looking at the priorities of the top, bottom, left, and right. If it is not a wall, put this point on the stack, and Treat this as the current node and repeat the cycle until the current point coordinate is equal to the end point coordinate and the maze is exited;

How to tell if the maze can be exited?

1) If the maze can be walked out, the current node coordinates are equal to the end point coordinates;

2) If the maze cannot be exited, the container that records the coordinates will be empty;

Note: When you reach a dead end and retreat, the retreated node must be popped off the stack until the next route;

2. Steps

2.1. Create a maze and confirm the starting point and end point

    # 创建迷宫
    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. Take out the current node and record it

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

2.3. Conditions for exiting the maze and determining whether each direction can be walked and recording the nodes.

        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()

3. Code

The complete code is as follows:



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. Results

4.1. Code running results

The result is longer, only part of it is intercepted, as shown in Figure 1 below:

figure 1

おすすめ

転載: blog.csdn.net/qq_57663276/article/details/128608484