Python basic tutorial: Python based on the recursive algorithm to achieve the maze problem

@This article comes from the public number: csdn2299, like to pay attention to the public number programmer academy
This article describes the problem of Python based on recursive algorithm to achieve the maze. Share with you for your reference, as follows:

What is recursion?

The simple understanding is that the process of function call itself is called recursion.

When is recursion used?

If a problem can be expressed as a smaller-scale iterative operation, a recursive algorithm can be used.
Maze problem: In a two-dimensional array composed of 0 or 1, suppose 1 is a point that can be moved to, and 0 is a point that cannot be moved. Move one unit in the left and right directions. When you move to the edge of the two-dimensional array, you can get the solution to the problem. Similar problems can be called maze problems.

You can use list nesting to represent a two-dimensional array in Python. Assuming a 6 * 6 maze, the problem starts from the array coordinates [3] [3], and judges whether it can successfully walk out of the maze.

maze=[[1,0,0,1,0,1],
   [1,1,1,0,1,0],
   [0,0,1,0,1,0],
   [0,1,1,1,0,0],
   [0,0,0,1,0,0],
   [1,0,0,0,0,0]]

For this maze problem, we can use the recursive idea to solve it well. For a point in the array, the four directions of the point can be easily expressed by the addition and subtraction of the horizontal and vertical coordinates. Whenever a movable point is moved, the entire problem becomes the same problem as the initial state. Continue to search Find the point that can be moved in four directions, knowing to move to the edge of the array.

So we can code like this:

# 判断坐标的有效性,如果超出数组边界或是不满足值为1的条件,说明该点无效返回False,否则返回True。
def valid(maze,x,y):
  if (x>=0 and x<len(maze) and y>=0 and y<len(maze[0]) and maze[x][y]==1):
    return True
  else:
    return False
# 移步函数实现
def walk(maze,x,y):
  # 如果位置是迷宫的出口,说明成功走出迷宫
  if(x==0 and y==0):
    print("successful!")
    return True
  # 递归主体实现
  if valid(maze,x,y):
    # print(x,y)
    maze[x][y]=2 # 做标记,防止折回
    # 针对四个方向依次试探,如果失败,撤销一步
    if not walk(maze,x-1,y):
      maze[x][y]=1
    elif not walk(maze,x,y-1):
      maze[x][y]=1
    elif not walk(maze,x+1,y):
      maze[x][y]=1
    elif not walk(maze,x,y+1):
      maze[x][y]=1
    else:
      return False # 无路可走说明,没有解 
  return True
walk(maze,3,3)

Recursion is a good thing!

Thank you very much for reading
. When I chose to study python at university, I found that I ate a bad computer foundation. I did n’t have an academic qualification. This is
nothing to do. I can only make up for it, so I started my own counterattack outside of coding. The road, continue to learn the core knowledge of python, in-depth study of computer basics, sorted out, if you are not willing to be mediocre, then join me in coding, and continue to grow!
In fact, there are not only technology here, but also things beyond those technologies. For example, how to be an exquisite programmer, rather than "cock silk", the programmer itself is a noble existence, isn't it? [Click to join] want to be yourself, want to be noble, come on

Published 50 original articles · 21 praises · 20,000+ views

Guess you like

Origin blog.csdn.net/chengxun03/article/details/105545232