Recursive application of the backtracking algorithm of Java data structure path problem of maze

Source: Internet
Author: User

First, Introduction

The basic idea of backtracking is to decompose the original problem into an algorithm for solving several sub-problems, including a number of nodes, each node having several search branches. When searching for a node and discovering that it is no longer possible to continue searching, let the search process backtrack (that is, return) to the previous node of the node, and continue searching for other branches of the node that have not yet been searched, and if the node is found to be unable to continue the search, Let the search process go back to the previous node of the node to continue the search process until the search has solved the problem or searched all the searchable branches without a solution to exist.

The method can use a stack implementation. You can also use recursive implementation, recursive implementation of the code is relatively simple, more difficult to understand the suggestion to break the point of a step-tracking understanding

//=============

1 /**2 * Backtracking method to solve, maze algorithm3  * 4  * @authorMIX5  * 6  */7  Public classMymaze {8 9     Private Static intStartposi;//i subscript of the entranceTen     Private Static intSTARTPOSJ;//J Subscript of the entrance One     Private Static intEndposi;//J Subscript for Exit A     Private Static intENDPOSJ;//J Subscript for Exit -  -     //set the coordinates of the maze entry the      Public voidSetstart (intStartposi,intstartposj) { -  -Mymaze.startposi =Startposi; -MYMAZE.STARTPOSJ =startposj; +     } -  +     //set the coordinates of the maze exit A      Public voidSetEnd (intEndposi,intendposj) { at  -Mymaze.endposi =Endposi; -MYMAZE.ENDPOSJ =endposj; -     } -  -     //algorithm of Maze search pathway in     /** -      *  to      * @paramCell + * Maze Map -      * @paramI the * The I coordinate of the entrance *      * @paramJ $ * The J-coordinate of the entrancePanax Notoginseng      */ -      Public Static voidVisited (int[] Cell,intIintj) { the         //indicates that the road is a pass +         /* A * This tag is recorded as passing through the path, once the path is a pathway and has reached the path then the position is set to 1 the          */ +CELL[I][J] = 1; -String oriention =NULL; $         if(i = = Endposi && j = = ENDPOSJ) {//found the exit. $System.out.println ("Find a Way"); -              for(intm = 0; M < cell.length; m++) { -                  for(intn = 0; n < cell[i].length; n++) { the                     if(Cell[m][n] = = 2) { -System.out.print ("2");Wuyi}Else if(Cell[m][n] = = 1) { theSystem.out.print ("*"); -}Else { WuSystem.out.print (""); -                     } About                 } $ System.out.println (); -             } -         } -  A         //find the path to the left +         if(Cell[i][j-1] = = 0) { theOriention = "Left"; -Visited (cell, I, j-1); $         } the         //find a path to the right the         if(cell[i][j + 1] = = 0) { theOriention = "Right"; theVisited (cell, I, J + 1)); -         } in         //find a path upward the         if(Cell[i-1][j] = = 0) { theOriention = "Up"; AboutVisited (cell, i-1, j); the         } the         //looking down the path the         if(Cell[i + 1][j] = = 0) { +Oriention = "Down"; -Visited (cell, i + 1, j); the         }Bayi         /* the * Once the path has reached a dead end, the path of cleanup passes the current position back to 1, launches the current recursive iteration stack, returns the last time, and continues the * The main application in the current function is to clean up the position that was marked as 1 before cleaning, and reset the position to 0 so that the next position can be accessed -          */ -Oriention = "Ignorant of Me"; theCELL[I][J] = 0; the  the     } the  -      Public Static voidMain (string[] args) { the         //Initialize a maze map the         int[] Maze = {{2, 2, 2, 2, 2, 2, 2, 2, 2 }, the{2, 0, 0, 0, 0, 0, 0, 0, 2}, {2, 0, 2, 2, 0, 2, 2, 0, 2 },94{2, 0, 2, 0, 0, 2, 0, 0, 2}, {2, 0, 2, 0, 2, 0, 2, 0, 2 }, the{2, 0, 0, 0, 0, 0, 2, 0, 2}, {2, 2, 0, 2, 2, 0, 2, 2, 2 }, the{2, 0, 0, 0, 0, 0, 0, 0, 2}, {2, 2, 2, 2, 2, 2, 2, 2, 2 } }; the 98         //Mymaze cell = new Mymaze (); About         //Cell.setstart (1, 1); -         //Cell.setend (7, 7);101         //cell.visited (Maze, Startposi, startposi);102         int[] Maze2 = {{2, 2, 2, 2}, {2, 0, 0, 2}, {2, 0, 0, 2 },103{2, 2, 2, 2 } };104Mymaze cell =NewMymaze (); theCell.setstart (1, 1);106Cell.setend (2, 2);107 cell.visited (Maze2, Startposi, STARTPOSJ);108     }109  the}

The test code in the above code I used a relatively simple 4*4 matrix to test, convenient breakpoint observation, understand the idea of the algorithm.

This is actually a bit similar to the method stack application of recursive methods.

Recursive application of the backtracking algorithm of Java data structure path problem of maze

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.