この記事では、スタックを使用して迷路アルゴリズムのJavaバージョンを共有しています。これは、主に参照のためのスタックの使用を調べます。特定のコンテンツは次のとおりです
主なアイデアは次のとおりです。
{if(現在の位置を渡すことができる){渡されたこの位置をマークします。現在の位置を保存し、スタックにマージします。 if(現在の位置はエンドポイントです){プログラムEND; }次の位置を取得します。 } else {if(スタックは空ではない){out of stack;一方(現在の位置方向は4で、スタックは空ではありません){移動できない現在の位置をマークします。スタックから; } if(現在の位置の方向は4未満){方向+1;スタックに再入力します。次の位置を取得します。 }}}} while(スタックは空ではありません);Javaコードは次のとおりです。
java.util.stack; public class maze {// stack private stack <mazenode> stack = new stack <maze.mazenode>(); // MAZE PRIVATE INT [] [] MAZE = {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1. {1,0,1,0,0,0,0,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0、 1,1,1,1,1,0,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0、 1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1,1,1、1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1、 0,0,1,0,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1、1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1、1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1、 、1,1、1、1、1,1,1、1、1、1,1,1,1,1,1,1、1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1. 、1,1、1、1、1,1,1、1、1、1,1,1,1,1,1,1、1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1. 、1,1、1、1、1,1,1、1、1、1,1,1,1,1,1,1、1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1. {1,0,0,1,1,0,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0, {1,1,0,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1. 、1,1、1、1、1,1、1、1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1、1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 、1,1、1、1、1,1、1、1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,プライベート静的最終int maze_size_x = 14;プライベート静的最終int maze_size_y = 17;プライベート静的最終int end_x = 12;プライベート静的最終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();位置curpos = new position(1、1); do {//このパスは(迷路[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()。方向); } // elseに移動できない{if(!stack.isempty()){mazenode curnode = stack.pop(); while(curnode.direction == 4 &&!stack.isempty()){//現在の位置の4つの方向すべてが試行された場合、移動できない位置をマークし、[curnode.position.x] [curnode.position.y] = 1; curnode = stack.pop(); } if(curnode.direction <4){curnode.direction ++; // direction +1 stack.push(curnode); // reinter curpos = nextpos(curnode.position、curnode.direction); //次の場所を取得}}}}}}}}}}}}}}}} } 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ノード; 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"); } //迷路クラスの位置のポイントの位置を記録{int x; int y;パブリックポジション(int x、int y){this.x = x; this.y = y; }} //スタッククラスのnode mazenode {position position; int方向; public mazenode(position pos){this.position = pos; } public mazenode(position pos、int dir){this.position = pos; this.direction = dir; }} //次の位置、右から始まる、時計回りのパブリック位置NextPos(位置、int方向){位置newposition = new position(position.x、position.y); switch(方向){ケース1:newposition.y += 1;壊す;ケース2:newposition.x += 1;壊す;ケース3:newposition.Y- = 1;壊す;ケース4:newposition.x - = 1;壊す;デフォルト:break; } newpositionを返します。 } public static void main(string [] args){迷路迷路= new Maze(); Maze.Drawmaze(); Maze.Process(); Maze.drawresult(); }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。