Python uses the Backtracking Method subset tree template to solve the Maze problem example, python maze

Source: Internet
Author: User

Python uses the Backtracking Method subset tree template to solve the Maze problem example, python maze

This article describes how Python uses the Backtracking Method to Solve the Maze problem. We will share this with you for your reference. The details are as follows:

Problem

Given a maze, the entrance is known. Check whether a path exists from the entrance to the exit. If yes, output a path like this. Note that you can move data from top, bottom, left, right, top left, top right, bottom left, and bottom right. Enter 0 in the maze to go, and 1 in the maze to represent the wall. For convenience, use 1 to enclose the maze to avoid boundary problems.

Analysis

Considering that the left and right directions are relative, the changes are as follows: North, Northeast, east, southeast, south, southwest, west, and northwest. In any format, you can select eight directions, that is, eight States. Therefore, from the entry lattice, each entry must traverse these eight States.

Obviously, the subset tree template of the backtracking method can be applied.

Note that the length of the solution is not fixed.

Code

# Maze (1 is the wall, 0 is the path) maze = [[,], [,],, ,], [,], [,] m, n = 8, 10 #8 rows, 10 columns of entry = () # maze entrance path = [entry] # A solution (path) paths = [] # A group of solutions # direction of movement (8 clockwise: N, EN, E, ES, S, WS, W, WN) directions = [(-), (-), (1,-1), (0,-1 ), (-1,-1)] # Conflict Detection Def conflict (nx, ny): global m, n, maze # Is it in the maze, and if 0 <= nx <m and 0 <= ny <n and maze [nx] [ny] = 0: return False return True # apply the subset tree template def walk (x, y): # Get (x, y) global entry, m, n, maze, path, paths, directions if (x, y )! = Entry and (x % m-1) = 0 or y % (n-1) = 0): # exit # print (path) paths. append (path [:]) # Save directly, without optimization else: for d in directions: # traverse eight directions (that is, eight States) nx, ny = x + d [0], y + d [1] path. append (nx, ny) # Save, new coordinates into stack if not conflict (nx, ny): # trim maze [nx] [ny] = 2 # mark, accessed (strange, these two sentences can only be placed in the if block !) Walk (nx, ny) maze [nx] [ny] = 0 # trace back and restore path. pop () # backtracking, output stack # solution visualization (based on a solution x, restore the maze path, '2' indicates the path) def show (path): global maze import pprint, copy maze2 = copy. deepcopy (maze) for p in path: maze2 [p [0] [p [1] = 2 # path pprint. pprint (maze) # original maze print () pprint. pprint (maze2) # maze with paths # test walk (1, 0) print (paths [-1], '\ n ') # Check the last path show (paths [-1]).

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.