Back-to-Back algorithm to solve the Maze problem (java Edition)

Source: Internet
Author: User

Back-to-Back algorithm to solve the Maze problem (java Edition)

A long matrix of M x N is used to represent the maze, and 0 and 1 represent the paths and obstacles in the maze, respectively. Design the program to find all the paths from the entrance to the exit for any preset maze.

The following describes in detail the backtracking algorithm of the Maze problem.

This is a labyrinth chart. 1 indicates that the wall cannot be taken, and 0 indicates that the route can be taken. You can only go up, down, and left until you exit from the upper left to the lower right corner.

The method is to use a two-dimensional array to define the initial state of the maze. Then, starting from the upper left corner, we constantly test all feasible routes. When we hit 1, we end the path and explore other directions, of course, we need to mark the routes we have already taken, so we cannot repeatedly go back and forth between two feasible grids. Until the exit is reached, a correct path is found.

The procedure is as follows. For more information, see annotations.

Package huisu;/*** Created by wolf on 2016/3/21. */public class MiGong {/*** defines the maze array */private int [] [] array = {0, 0, 1, 0, 0, 0, 1, 0}, {0, 0, 1, 0, 0, 0, 1, 0}, {0, 0, 1, 0, 1, 0, 1 }, {0, 1, 1, 1, 0, 0, 1, 0}, {0, 0, 0, 1, 0, 0, 0}, {0, 1, 0, 0, 0, 1, 0, 1}, {0, 1, 1, 1, 1, 0, 0, 1}, {1, 1, 0, 0, 0, 1, 0, 1}, {1, 1, 0, 0, 0, 0, 0}; private int maxLine = 8; private int maxRow = 9; public static void main (String [] args) {System. out. println (System. currentTimeMillis (); new MiGong (). check (0, 0); System. out. println (System. currentTimeMillis ();} private void check (int I, int j) {// if the exit at the bottom right corner is reached if (I = maxRow-1 & j = maxLine-1) {print (); return;} // returns to the right if (canMove (I, j, I, j + 1) {array [I] [j] = 5; check (I, j + 1); array [I] [j] = 0;} // walk left if (canMove (I, j, I, j-1 )) {array [I] [j] = 5; check (I, j-1); array [I] [j] = 0 ;} // go down to if (canMove (I, j, I + 1, j) {array [I] [j] = 5; check (I + 1, j ); array [I] [j] = 0;} // go up to if (canMove (I, j, I-1, j )) {array [I] [j] = 5; check (I-1, j); array [I] [j] = 0 ;}} private boolean canMove (int I, int j, int targetI, int targetJ) {// System. out. println ("from column" + (I + 1) + "row" + (j + 1) + ", to column" + (targetI + 1) + "Row No." + (targetJ + 1) + "column"); if (targetI <0 | targetJ <0 | targetI> = maxRow | targetJ> = maxLine) {// System. out. println ("to the leftmost or rightmost, failed"); return false;} if (array [targetI] [targetJ] = 1) {// System. out. println ("the target is a wall, failed"); return false;} // avoid going back and forth between two spaces if (array [targetI] [targetJ] = 5) {// System. out. println ("back and forth, failed"); return false;} return true;} private void print () {System. out. println ("get a solution:"); for (int I = 0; I <maxRow; I ++) {for (int j = 0; j <maxLine; j ++) {System. out. print (array [I] [j] + "");} System. out. println ();}}}
I commented out the locations for printing the path judgment in each step, and opened the comments to see all the paths.
The program execution efficiency is very fast. Basically, all paths are obtained within 3 ms.

 

I thought there were only three paths when I only looked at the figure. I didn't expect the program to output eight. Later, let's take a closer look. There are 8 paths ......

The print result is as follows. 5 is used to mark the path:

 

1458551044499 get a solution: 5 5 1 0 0 0 1 0 5 5 1 0 0 1 0 0 5 0 1 0 1 1 1 0 1 1 1 1 1 0 0 1 0 5 5 5 5 5 5 5 5 0 0 1 5 5 5 1 1 0 1 1 1 0 5 1 1 0 0 0 0 1 1 5 1 1 1 1 1 0 0 0 5 0 get a solution: 5 5 1 0 0 0 1 0 5 5 1 0 0 1 0 5 0 1 0 1 1 1 1 0 1 1 1 1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 0 0 1 5 5 5 1 1 0 1 1 1 0 5 1 1 0 0 0 0 1 1 5 1 1 1 1 1 0 0 0 5 0 get a solution: 5 5 1 0 0 0 1 0 5 1 0 0 0 1 0 5 5 5 1 0 1 1 1 0 1 1 1 1 0 0 1 0 5 5 5 5 5 5 5 5 5 0 0 1 5 5 5 1 1 0 1 1 1 0 5 1 1 0 0 0 0 1 1 5 1 1 1 1 1 0 0 0 5 0 get a solution: 5 5 1 0 0 0 1 0 5 1 0 0 0 1 0 5 5 5 1 0 1 1 0 1 1 5 1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 0 0 1 5 5 5 1 1 0 1 1 1 0 5 1 1 0 0 0 0 1 1 5 1 1 1 1 1 0 0 0 5 0 get a solution: 5 0 1 0 0 0 1 0 5 5 1 0 0 1 0 5 5 5 1 0 1 1 1 0 1 1 1 1 0 0 1 0 5 5 5 5 5 5 5 5 5 0 0 1 5 5 5 1 1 0 1 1 1 0 5 1 1 0 0 0 0 1 1 5 1 1 1 1 1 0 0 0 5 0 get a solution: 5 0 1 0 0 0 1 0 5 5 1 0 0 1 0 5 5 5 1 0 1 1 0 1 1 5 1 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 0 0 1 5 5 5 1 1 0 1 1 1 0 5 1 1 0 0 0 0 1 1 5 1 1 1 1 1 0 0 0 5 0 get a solution: 5 0 1 0 0 0 1 0 0 1 0 0 0 1 0 5 0 1 0 1 1 1 1 0 1 1 1 1 0 0 1 0 5 5 5 5 5 5 5 5 0 0 1 5 5 5 1 1 0 1 1 1 0 5 1 1 0 0 0 0 1 1 5 1 1 1 1 1 0 0 0 5 0 get a solution: 5 0 1 0 0 0 1 0 0 1 0 0 0 1 0 5 0 1 0 1 1 1 1 0 1 1 1 1 1 5 5 5 1 0 5 5 5 5 5 5 5 5 5 0 0 1 5 5 5 1 1 0 1 1 1 1 0 5 1 1 0 0 0 1 1 1 1 1 0 0 0 5 0 1458551044503

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.