Java implements the maze backtracking algorithm, and java adopts the maze backtracking algorithm.

Source: Internet
Author: User

Java implements the maze backtracking algorithm, and java adopts the maze backtracking algorithm.

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 a program to find a path from the entrance to the exit of any preset maze, or draw a conclusion that there is no path.
(1) outputs the maze chart based on the two-dimensional array.
(2) Four Directions of the exploration maze: RIGHT: DOWN: LEFT, UP: output the walking path from the entrance to the exit.

Example:
In the upper left corner (), the entrance is used, and in the lower right corner (), the exit is used.

You can use the backtracking method, that is, to start from the entrance and explore in a certain direction. If you can go through, continue to move forward; otherwise, you will return along the original road and continue to explore in another direction, obtain a path at the exit position. If all possible paths are explored and cannot reach the exit, the specified Maze has no paths.

Import java. util. *; class Position {public Position () {} public Position (int row, int col) {this. col = col; this. row = row;} public String toString () {return "(" + row + "," + col + ")" ;}int row; int col ;} class Maze {public Maze () {maze = new int [15] [15]; stack = new Stack <Position> (); p = new boolean [15] [15];}/** constructor */public void init () {empty variable = new variable (System. in); System. ou T. println ("Enter the number of rows in the Maze"); row = rows. nextInt (); System. out. println ("Enter the number of columns in the Maze"); col = upper. nextInt (); System. out. println ("enter" + row + "row" + col + "column Maze"); int temp = 0; for (int I = 0; I <row; ++ I) {for (int j = 0; j <col; ++ j) {temp = temp. nextInt (); maze [I] [j] = temp; p [I] [j] = false ;}}/** backtrack the maze, check whether there is a way out */public void findPath () {// a circle of wall int temp [] [] = new int [row + 2] [col + 2]; for (int I = 0; I <row + 2; ++ I) {for (int j = 0; j <col + 2; ++ j) {temp [0] [j] = 1; temp [row + 1] [j] = 1; temp [I] [0] = temp [I] [col + 1] = 1 ;}/// copy the original maze to the new one for (int I = 0; I <row; ++ I) {for (int j = 0; j <col; ++ j) {temp [I + 1] [j + 1] = maze [I] [j] ;}// query int I = 1 clockwise from the upper left corner; int j = 1; p [I] [j] = true; stack. push (new Position (I, j); while (! Stack. empty ()&&(! (I = (row) & (j = col) {if (temp [I] [j + 1] = 0) & (p [I] [j + 1] = false) {p [I] [j + 1] = true; stack. push (new Position (I, j + 1); j ++;} else if (temp [I + 1] [j] = 0) & (p [I + 1] [j] = false) {p [I + 1] [j] = true; stack. push (new Position (I + 1, j); I ++;} else if (temp [I] [j-1] = 0) & (p [I] [j-1] = false) {p [I] [j-1] = true; stack. push (new Position (I, j-1); j --;} else I F (temp [I-1] [j] = 0) & (p [I-1] [j] = false )) {p [I-1] [j] = true; stack. push (new Position (I-1, j); I --;} else {stack. pop (); if (stack. empty () {break;} I = stack. peek (). row; j = stack. peek (). col ;}} Stack <Position> newPos = new Stack <Position> (); if (stack. empty () {System. out. println ("no path");} else {System. out. println ("with path"); System. out. println ("Path:"); while (! Stack. empty () {Position pos = new Position (); pos = stack. pop (); newPos. push (pos) ;}/ ** graphical output path **/String resault [] [] = new String [row + 1] [col + 1]; for (int k = 0; k <row; ++ k) {for (int t = 0; t <col; ++ t) {resault [k] [t] = (maze [k] [t]) + "" ;}} while (! NewPos. empty () {Position p1 = newPos. pop (); resault [p1.row-1] [p1.col-1] = "#" ;}for (int k = 0; k <row; ++ k) {for (int t = 0; t <col; ++ t) {System. out. print (resault [k] [t] + "\ t");} System. out. println () ;}} int maze [] []; private int row = 9; private int col = 8; Stack <Position> stack; boolean p [] [] = null;} class hello {public static void main (String [] args) {Maze demo = new Maze (); demo. init (); demo. findPath ();}}

Running example:

Enter the number of rows in the maze.
3
Enter the number of columns in the maze.
3
Enter a maze of 3 rows and 3 columns
0 1 1
0 0 1
1 0 0

Path available
Path:

Enter the number of rows in the maze.
9
Enter the number of columns in the maze.
8
Enter a maze of 9 rows and 8 Columns
0 0 1 0 0 1 0
0 0 1 0 0 1 0
0 0 1 0 1 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 0

Path available
Path:

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

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.