이 기사는 스택을 사용하여 Maze 알고리즘의 Java 버전을 공유하며, 주로 참조 스택 사용을 검사합니다. 특정 내용은 다음과 같습니다
주요 아이디어는 다음과 같습니다.
do {if (현재 위치가 통과 될 수 있음) {통과 된이 위치를 표시하십시오. 현재 위치를 저장하고 스택에 병합하십시오. if (현재 위치는 종점입니다) {프로그램 종료; } 다음 위치를 얻으십시오. } else {if (스택이 비어 있지 않음) {스택에서; 반면 (현재 위치 방향은 4이고 스택이 비어 있지 않음) {이동할 수없는 현재 위치를 표시하십시오. 스택 밖으로; } if (현재 위치의 방향은 4 미만) {Direction +1; 스택을 다시 입력하십시오. 다음 위치를 얻으십시오. }}}} while (스택은 비어 있지 않음);Java 코드는 다음과 같습니다.
import 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,0,1,0,0,0,1,1,1,1,1,1,1}, {1,0,1,1,0,0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,1,1,1,1,1,1,1,0, 1,1,1,1,0,0,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0, 1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1,1,1,1,1, 0,0,1,0,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1, 1,1,1,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,0,0,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1}, {1,1,0,1,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, 개인 정적 최종 최종 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 = 새로운 위치 (1, 1); {//이 경로는 (maze [curpos.x] [curpos.y] == 0 && mark [curpos.x] [curpos.y] == 0) {mark [curpos.x] [curpos.y] = 1; stack.push (새로운 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.y] = 1; Curnode = stack.pop (); } if (curnode.direction <4) {curnode.direction.direction.direction ++; // direction +1 stack.push (curnode); // reenter curpos = nextpos (curnode.position, curnode.direction); // 다음 위치}}}} while (! stack.isempty ()); } public void drawmaze () {for (int i = 0; i <maze.length; i ++) {for (int j = 0; } 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; } system.out.print ( "/n"); } system.out.print ( "/n"); } // 미로 클래스 위치에서 점의 위치를 기록합니다 {int x; in y; 공공 위치 (int x, int y) {this.x = x; this.y = y; }} // 스택 클래스의 노드 Mazenode {위치 위치; int 방향; public mazenode (위치 pos) {this.position = pos; } public mazenode (위치 pos, int dir) {this.position = pos; 이것은 방향 = dir; }} // 다음 위치, 오른쪽에서 시작하여 시계 방향 공공 위치 Nextpos (위치 위치, int 방향) {위치 newPosition = new 위치 (position.x, position.y); 스위치 (방향) {사례 1 : newPosition.y += 1; 부서지다; 사례 2 : newPosition.x += 1; 부서지다; 사례 3 : newPosition.y- = 1; 부서지다; 사례 4 : newPosition.x- = 1; 부서지다; 기본값 : 브레이크; } 신규 위치를 반환합니다. } public static void main (String [] args) {Maze Maze = New Maze (); Maze.DrawMaze (); maze.process (); maze.drawresult (); }}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.