One python uses the maze algorithm question, while the python uses the maze algorithm question.

Source: Internet
Author: User

One python uses the maze algorithm question, while the python uses the maze algorithm question.

I saw this problem when I visited my blog a few days ago and it was interesting. I thought about the solution and implemented it using python. The questions are as follows:

A two-dimensional array is used to represent a simple maze. 0 represents a path and 1 represents a blocking. Rats can move four neighboring Southeast-Northwest points on each point to design an algorithm, simulate the mouse to walk through the maze and find a path from the entrance to the exit.

:

Let's talk about my ideas first:

1. First, use a list source to store the maze map, a list route_stack storage roadmap, and a list route_history to store the vertices that have elapsed, starting point (), and ending point ).

2. The mouse has four options at each point. You need to define the implementation methods of these solutions.

3. Make a loop at the end. If the current vertex is not (), execute the top, bottom, and left methods in sequence, but there are some restrictions. For example, the vertex that you try will not try again. (0, x) points cannot be executed.

Paste the following code:

# _ * _ Coding: UTF-8 _ * _ route_stack = [[] route_history = [[] source = [[, 1, 1], [, 0], [, 0, 0], [, 0] def up (location): # the abscissa is 0, you cannot go up to if location [1] = 0: return False else: new_location = [location [0], location [1]-1] # if new_location in route_history: return False # When a wall is hit, elif source [new_location [0] [new_location [1] = 1: return False else: route_s Tack. append (new_location) route_history.append (new_location) return True def down (location): if location [1] = 4: return False else: new_location = [location [0], location [1] + 1] if new_location in route_history: return False elif source [new_location [0] [new_location [1] = 1: return False else: route_stack.append (new_location) route_history.append (new_location) return True def left (location): if loc Ation [0] = 0: return False else: new_location = [location [0]-1, location [1] if new_location in route_history: return False elif source [new_location [0] [new_location [1] = 1: return False else: route_stack.append (new_location) route_history.append (new_location) return True def right (location ): if location [0] = 4: return False else: new_location = [location [0] + 1, location [1] if new_location in ro Ute_history: return False elif source [new_location [0] [new_location [1] = 1: return False else: route_stack.append (new_location) route_history.append (new_location) return True lo = [0, 0] while route_stack [-1]! = [4, 4]: if up (lo): lo = route_stack [-1] continue if down (lo): lo = route_stack [-1] continue if left (lo ): lo = route_stack [-1] continue if right (lo): lo = route_stack [-1] continue route_stack.pop () lo = route_stack [-1] print route_stack

The execution result is as follows:


The source of the question has another way of thinking, but I feel a little annoyed. I have a better understanding of it and it is easier to implement it.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.