Backtracking to solve the Maze problem

Source: Internet
Author: User
: This article mainly introduces the backtracking method to solve the Maze problem. if you are interested in the PHP Tutorial, please refer to it. Introduction

I recently read some algorithm questions on leetcode, some of which are very simple and commonly used, and I suddenly cannot figure out how to solve them, for example:Implement sqrt functions,Array arrangement. If high mathematics is not good, these seemingly simple problems will also feel very difficult to solve for the first time. of course, what we want to talk about today is to solve all the solutions of the maze, the backtracking method is used to solve this problem. if you do not understand this idea, it is difficult to solve many complicated problems.

Problem Description

This problem was encountered during a visit, but it is hard to remember where it is.

1   1   1   10   1   0   10   1   0   10   1   1   1

The above is a maze, the upper left is the entrance, the lower right is the exit, Xiao Meng (yes, you are not mistaken, it is long grass James) from the entrance, escape from the exit (X monsters will eat them if they cannot escape within one hour). 1 indicates that they can pass, 0 indicates that they cannot pass, and they can only go to the right and down directions, find all possible routes for Xiao Meng to escape.

This question seems simple. you can see the answer at a moment, but you don't know where to start by translating your thoughts into code.


One solution to this problem is the backtracking method. First, let's look at the definition of the backtracking method (Baidu Encyclopedia:

The backtracking method (exploration and backtracking) is an optimization search method, also known as the testing method. it searches forward based on the selection conditions to achieve the goal. However, when you find that the previous selection is not optimal or fails to reach the target, you can return to the previous step and re-select the target. this technology will return to the backtracing method if you fail to get the target, vertices that meet backtracing conditions are called backtracing points ".

My ideas:

  • Coordinates the maze above, with the upper left corner (0, 0) and lower right corner (3, 3). Other points are scattered in the coordinate system.
  • Starting from (0, 0)
  • Start from the given coordinate point and search right first. if it is 1, continue. if it is 0, search down. record the coordinates already searched before the search.
  • When the coordinate is equal to (3, 3), it is a backtracking point. at this time, it also returns
  • Repeat step 3 as long as the domain name does not cross the border.

See my PHP implementation:

  $ XL | $ y> $ yL) {// you can run to a space that does not exist in the maze. return is definitely not allowed ;} if ($ data [$ x] [$ y] = "0") {// if it is 0, stop and move forward. return to the previous status ;} elseif ($ data [$ x] [$ y] = "1") {// if it is 1, record the latest coordinates to the path already found, continue to search forward // if it reaches the exit, record the answer and trace back to $ snapshort = array_merge ($ record, [[$ x, $ y]); if ($ x = $ xL & $ y = $ yL) {$ result [] = array_merge ($ record, [[$ x, $ y]); return ;}} else {return ;}// saved the status of the current search location to $ snapshort in a specific search. getRet ($ data, $ x, ++ $ y, $ result, $ snapshort); // search getRet ($ data, ++ $ x, -- $ y, $ result, $ snapshort);} // See the example $ result = []; getRet ($ nums, 0, 0, $ result, []); foreach ($ result as $ pos) {foreach ($ pos as $ xy) {echo "({$ xy [0]}, {$ xy [1]}) => ";} echo "end \ n" ;}// output result () =>) => () => end () =>) => (1, 3) => (2, 3) => (3, 3) => (3, 5) => end (0, 0) => (0, 1) => () => end

The above introduces the backtracking method to solve the Maze problem, including some content, and hope to be helpful to friends who are interested in the PHP Tutorial.

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: 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.