Java version of the stack-based maze algorithm and java code of the Maze Algorithm
This article shares with you the java version of the Maze Algorithm Using stacks. It mainly describes how to use stacks for your reference. The specific content is as follows:
The main idea is as follows:
Do {if (the current position can be passed) {mark this position has passed; Save the current position to be merged into the stack; if (the current position is the end point) {program ends;} get the next position ;} else {if (the stack is not empty) {outbound stack; while (the current position is 4 and the stack is not empty) {mark the current position as unavailable; outbound stack ;} if (the direction of the current position is less than 4) {direction + 1; re-import stack; obtain the next position; }}while (the stack is not empty );
The java code is as follows:
Import java. util. stack; public class Maze {// Stack private stack <MazeNode> Stack = new Stack <Maze. mazeNode> (); // maze private int [] [] maze = {, 1,, 1}, {, 1,, 1}, {, 0, 1,, 1}, {, 1}, {1, ,}, {, 1,, 1}, {, 0, 0, 0, 0, 1,, 1}, {, 0, 1, 1,, 1 },}; // indicates whether the path has passed the private int [] [] mark = new int [MAZE_SIZE_X] [MAZE_SIZE_Y]; private static final int MAZE_SIZE_X = 14; private static final int MA ZE_SIZE_Y = 17; private static final int END_X = 12; private static final int END_Y = 15; private void initMark () {for (int I = 0; I <MAZE_SIZE_X; I ++) {for (int j = 0; j <MAZE_SIZE_Y; j ++) {mark [I] [j] = 0 ;}} public void process () {initMark (); Position curPos = new Position (1, 1); do {// This path can go if (maze [curPos. x] [curPos. y] = 0 & mark [curPos. x] [curPos. y] = 0) {mark [curPos. x] [curPos. y] = 1; stack. push (new MazeNode (curPos, 1); // if (curPos. x = END_X & curPos. y = END_Y) {return;} curPos = nextPos (curPos, stack. peek (). direction);} // else {if (! Stack. isEmpty () {MazeNode curNode = stack. pop (); while (curNode. direction = 4 &&! Stack. isEmpty () {// if all four directions of the current position have been tried, mark the position as unavailable and exit the stack mark [curNode. position. x] [curNode. position. y] = 1; curNode = stack. pop ();} if (curNode. direction <4) {curNode. direction ++; // direction + 1 stack. push (curNode); // re-import stack curPos = nextPos (curNode. position, curNode. direction); // get the next position }}} while (! Stack. isEmpty ();} public void drawMaze () {for (int I = 0; I <maze. length; I ++) {for (int j = 0; j <maze [0]. length; j ++) {System. out. print (maze [I] [j]);} System. out. print ("\ n");} System. out. print ("\ n");} public void drawResult () {initMark (); MazeNode node; while (! Stack. isEmpty () {node = stack. pop (); mark [node. position. x] [node. position. y] = 1 ;}for (int I = 0; I <mark. length; I ++) {for (int j = 0; j <mark [0]. length; j ++) {System. out. print (mark [I] [j]);} System. out. print ("\ n");} System. out. print ("\ n") ;}// records the Position of points in the Maze. class Position {int x; int y; public Position (int x, int y) {this. x = x; this. y = y ;}// class MazeNode {Position position; int direction; public MazeNode (Position pos) {this. position = pos;} public MazeNode (Position pos, int dir) {this. position = pos; this. direction = dir ;}// the next Position, starting from the right, clockwise public Position nextPos (position Position, int direction) {Position newPosition = new position (Position. x, position. y); switch (direction) {case 1: newPosition. y + = 1; break; case 2: newPosition. x + = 1; break; case 3: newPosition. y-= 1; break; case 4: newPosition. x-= 1; break; default: break;} return newPosition;} public static void main (String [] args) {Maze maze = new Maze (); maze. drawMaze (); maze. process (); maze. drawResult ();}}
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.