This article mainly introduced the PHP implementation of the maze problem based on backtracking method, combined with examples of the principle of backtracking, implementation steps and solve the maze problem related operation skills, the need for friends can refer to the next
In this paper, we describe the method of solving maze problem based on backtracking. Share to everyone for your reference, as follows:
Introduction
Recently in the Leetcode to see some algorithmic problems, some look at a very simple very common things, unexpectedly can not come out how to solve, such as: to implement the SQRT function, array arrangement. If the high math is not good, these seemingly simple problems, the first encounter will also feel difficult to solve, of course, today is to say is such a problem, solve the maze of all the solution, the problem of the use of backtracking method of thinking, do not understand the idea, a lot of slightly more complex problems are difficult to solve.
Problem description
This problem is in the real blind time encountered, where the specific record is not very clear.
1 1 1 1
0 1 0 1
0 1 0 1
0 1 1 1
Above is a maze, the upper left corner is the entrance, the lower right corner is the exit, Xiao Meng (yes, you did not see the wrong, is a long grass of xiaoming) from the entrance into, from the exit escape (1 hours will be eaten by the X Monster), of which 1 means that can be used, 0 is not accessible, only right and down two Find out all the little moe possible escape routes.
The question seems simple enough to see the answer, but translating the idea into code doesn't know where to start.
How to Solve
One solution to this problem is backtracking, first look at the definition of the backtracking method (Baidu Encyclopedia):
Backtracking (exploration and backtracking) is an optimal search method, also called the Heuristic method, which is searched forward according to the selection criteria to achieve the goal. However, when the exploration of a step, found that the original choice is not good or not reach the goal, then return to the one-step re-selection, such a failure to return to go back to the technology as backtracking, and meet the backtracking condition of a state point of the "backtracking point."
My train of thought:
1. Coordinate the above maze, the upper left corner is (0,0), the lower right corner is (3,3), the other points are scattered in the coordinate system
2. Starting from (0,0)
3. Starting from the given coordinate point, first to the right, is 1 words continue, is 0 words down search, before the search records the currently searched coordinates
4. When the coordinate equals (3,3) is a backtracking point, this time also return
5. Repeat the third step as long as you don't cross the border
Take a look at my PHP implementation:
<?php$nums = [[1,1,1,1,1,1], [0,1,0,1,0,1], [0,1,0,1,0,1], [0,1,1,1,1,1]]; function Getret ($data, $x, $y, & $result =[], $record) {$snapshort = []; $xL = count ($data)-1; $yL = count ($data [0])-1; if ($x > $xL | | $y > $yL) {//run to a space where the maze does not exist, this kind of thing can never happen return; if ($data [$x] [$y] = = "0") {//is 0 then stop moving forward and return to the previous state return; } elseif ($data [$x] [$y] = = "1") {//is 1, record the latest coordinates to the currently found path, continue to search forward//If the exit is reached, record the answer and backtrack $snapshort = Array_merge ($recor D, [[$x, $y]]); if ($x = = $xL && $y = = $yL) {$result [] = Array_merge ($record, [[$x, $y]]); Return }} else {return; }//To have a search//here $snapshort Save the status of the current search location, until the next time back to here will use Getret ($data, $x, + + $y, $result, $snapshort); Search down Getret ($data, + + $x,--$y, $result, $snapshort);} See an example $result = [];getret ($nums, 0, 0, $result, []); foreach ($result as $pos) {foreach ($pos as $xy) {echo "({$xy [0] },{$xy [1]}) = "; } echo "end\n";}
Output results
(0,0) = (0,1) = (0,2) and (0,3) = (0,4) = (0,5) = (1,5) = (2,5) = (3,5) =>end (0,0) = (0,1) = > (0,2) and (0,3) = (1,3) and (2,3) = (3,3) = (3,4) = (3,5) =>end (0,0) = (0,1) and (+) = ( 2, 1) = (3,1) = (3,2) = (3,3) = (3,4) = (3,5) =>end