Python is implemented based on Recursive Algorithms and python is implemented based on the maze.

Source: Internet
Author: User

Python is implemented based on Recursive Algorithms and python is implemented based on the maze.

This example describes how to use a recursive algorithm to perform a Python maze. We will share this with you for your reference. The details are as follows:

What is recursion?

Simply put, the process of function calling itself is called recursion.

When Will recursion be used?

If a problem can be expressed as a smaller iteration, a recursive algorithm can be used.
Maze problem: in a two-dimensional array composed of 0 or 1, assume that 1 is a point that can be moved, and 0 is a point that cannot be moved, how to start from a point with a value of 1 in the array, each of which can only move one unit to the top, bottom, left, and right directions. when moving to the edge of a two-dimensional array, the problem can be solved, similar problems can be called maze problems.

In python, list nesting can be used to represent two-dimensional arrays. Suppose a 6*6 maze. In the case of a problem, the array coordinates [3] [3] determine whether the maze can be successfully obtained.

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

To solve this Maze problem, we can use recursive thinking. For a vertex in the array, the four directions of the vertex can be easily represented by addition and subtraction of the horizontal and vertical coordinates. When a movable vertex is moved, the entire problem becomes the same as the initial state, continue to search for moving points in four directions and find the points to be moved to the edge of the array.

So we can encode it like this:

# Determine the validity of coordinates. If the value of coordinates exceeds the array boundary or the value of 1 is not met, False is returned if the value of 1 is invalid. Otherwise, True is returned. 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 # walk function implementation def walk (maze, x, y ): # if the location is the exit of the maze, it indicates that the maze is successfully exited. if (x = 0 and y = 0): print ("successful! ") Return True # recursive subject implementation if valid (maze, x, y): # print (x, y) maze [x] [y] = 2 # mark, prevent fold back # test in four directions in turn, if the failure, undo a step 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 # no path to walk, no return Truewalk (maze, 3, 3)

Recursion is a good thing!

PS: there is also an infinite maze game on this site. It is implemented based on JS and is provided for your reference:

Online maze games:
Http://tools.jb51.net/games/migong

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.