BFS solves the shortest path of the maze

1 Overview

This is the first time I write an article, and the subject matter is the BFS algorithm that I have just started. There are many articles about this kind of algorithm on the blog, and there are many simple and fast algorithms. I use a method with ordinary knowledge. I mainly want to share the errors encountered in the code writing process , hoping to give you some lessons.

2. The logic of the algorithm

When I first wanted to solve the maze problem, I read a lot of code and tried to quickly learn and master this algorithm (I am a novice). Not only did I not understand it, but it made me even more anxious. Later, I watched some explanation videos and realized that I wanted to master the new algorithm. Algorithms, first of all, you need to understand the logic of this algorithm, and preview the process of algorithm operation in the form of charts and texts. After understanding the algorithm logic, you can start writing code.
The idea of ​​BFS to solve the maze problem is to start from the starting point, record the points that can be reached in each step, and judge whether the latest point is the end point, if it is the end point, stop the search, otherwise continue to go down one step. There are two problems to be solved here. First, solve the points that can be reached by one step at any point, and create a list to store the starting point and potential end point (there are four ways to move up, down, left and right for each point, if the point that can be reached does not exceed range, not a wall and not traversed, considered a potential end point). Second, solve another problem. After completing the search of a point, it will search for the next point added to the list, and loop until the potential end point is the end point.

3. Code Analysis

The code is divided into three parts, Part 1 is a two-dimensional matrix map imported from a Txt into the maze, as shown below, and the row and column values ​​of the matrix are
0 0 1 1 0
0 0 0 1 0
1 1 0 0 0
1 0 1 1 0
0 0 0 0 0
1 0 1 1 0
Part 2 is to establish five lists, Dir stores the running orientation of each point, x, y and s store the x-coordinate, y-coordinate and number of steps of each point in the search process, idx stores the number of points that have been traversed.
Part 3 is the subject part of the code, the loop determines whether the potential end point is the end point, and if so, stops the loop.

#Part 1
f = open('Migong.txt','r')
Mg = []
for line in f:
    li = list(map(int,line.split()))
    Mg.append(li)
a =len(Mg)
b = len(Mg[0])
#Part 2
Dir = [[-1,0],[0,1],[1,0],[0,-1]]
x = []
y = []
s = []
idx = []
Vis = [[0 for i in range(b)]for i in range(a)]
x.append(0)
y.append(0)
s.append(0)
Vis[0][0] = 1
step = 0
index = 0
#Part 3
while (x[index] + y[index] < a+b-2):
    for i in range(4):
        Nx = x[index] + Dir[i][0]
        Ny = y[index] + Dir[i][1]
        if Nx < 0 or Nx > a-1 or Ny<0 or Ny>b-1:
            continue
        if Mg[Nx][Ny] == 1 or Vis[Nx][Ny] == 1:
            continue
        x.append(Nx)
        y.append(Ny)
        s.append(step+1)
        Vis[Nx][Ny] = 1
    idx.append(index) #记录遍历过的每一个点
    index +=1
    step = s[index]
print(s[-1])

4. Errors encountered during code writing

The coding process spends the most time recording the relationship between each potential end point and the point currently being traversed. The total number of steps to reach each potential end point is the number of points being traversed + 1, and if you understand this, you can successfully complete the algorithm!
This is the first time to write an article. If there is something wrong with the writing, please correct me!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324136363&siteId=291194637