使用栈的迷宫算法java版代码

作者:young_leez 时间:2022-03-07 12:47:16 

本文为大家分享了使用栈的迷宫算法java版,主要考察栈的使用,供大家参考,具体内容如下

主要思路如下:


do {
if(当前位置可通过) {
 标记此位置已走过;
 保存当前位置并入栈;
 if(当前位置为终点) {
  程序结束;
 }
 获取下一个位置;
}
else {
 if(栈非空) {
  出栈;
  while(当前位置方向为4且栈非空) {
   标记当前位置不可走;
   出栈;
  }
  if(当前位置的方向小于4) {
   方向+1;
   重新入栈;
   获取下一个位置;
  }
 }
}
}
while (栈非空);

java代码如下:


import java.util.Stack;

public class Maze {

// 栈
private Stack<MazeNode> stack = new Stack<Maze.MazeNode>();
// 迷宫
private int[][] maze = {
 {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,0,0,0,1,1,1,1,1,1},
 {1,0,0,0,0,1,1,0,1,1,1,0,0,1,1,1,1},
 {1,0,1,1,0,0,0,0,1,1,1,1,0,0,1,1,1},
 {1,1,1,0,0,1,1,1,1,1,1,0,1,1,0,0,1},
 {1,1,0,0,1,0,0,1,0,1,1,1,1,1,1,1,1},
 {1,0,0,1,1,1,1,1,1,0,1,0,0,1,0,1,1},
 {1,0,0,1,1,1,1,1,1,0,1,0,0,1,0,1,1},
 {1,0,1,1,1,0,0,0,0,1,1,1,1,1,1,1,1},
 {1,0,0,1,1,0,1,1,0,1,1,1,1,1,0,1,1},
 {1,1,0,0,0,0,1,1,0,1,0,0,0,0,0,0,1},
 {1,1,0,1,1,1,1,1,0,0,0,1,1,1,1,0,1},
 {1,0,0,0,0,1,1,1,1,1,0,1,1,1,1,0,1},
 {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
};
// 标记路径是否已走过
private int[][] mark = new int[MAZE_SIZE_X][MAZE_SIZE_Y];

private static final int MAZE_SIZE_X = 14;
private static final int MAZE_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 {
  // 此路径可走
  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()) {
     // 如果当前位置的4个方向都已试过,那么标记该位置不可走,并出栈
     mark[curNode.position.x][curNode.position.y] = 1;
     curNode = stack.pop();
    }
    if (curNode.direction < 4) {
     curNode.direction++;// 方向+1
     stack.push(curNode);// 重新入栈
     curPos = nextPos(curNode.position, curNode.direction);// 获取下一个位置
    }
   }
  }
 }
 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");
}

// 记录迷宫中的点的位置
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;
 }
}

// 下一个位置,从右开始,顺时针
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();
}

}

来源:http://blog.csdn.net/Young_Leez/article/details/44026295

标签:java,迷宫算法
0
投稿

猜你喜欢

  • eclipse端口被占用问题的解决方法

    2022-10-04 07:01:54
  • Java 多用户登录限制的实现方法

    2022-04-06 07:32:46
  • C#数组初始化简析

    2022-01-02 11:55:12
  • Android手势识别器GestureDetector使用详解

    2022-01-16 14:25:17
  • SpringBoot整合Pulsar的实现示例

    2021-10-09 17:39:35
  • Android基于Xposed修改微信运动步数实例

    2022-11-06 19:46:57
  • java 二分法算法的实例

    2023-04-25 05:04:05
  • SpringBoot启动自动终止也不报错的原因及解决

    2023-01-16 02:15:40
  • 快速入门介绍Java中强大的String.format()

    2022-10-28 15:53:49
  • 5分钟用C#实现串口助手

    2022-01-20 09:43:00
  • Android中ADB命令用法大结局

    2022-12-18 10:36:28
  • Android自定义控件实现支付宝记账饼图

    2022-04-19 13:27:02
  • 深入浅析jni中的java接口使用

    2023-07-22 19:54:23
  • 实例分析Android中HandlerThread线程用法

    2022-05-25 23:34:13
  • java 验证用户是否已经登录与实现自动登录方法详解

    2021-10-21 13:49:50
  • 基于java中两个对象属性的比较

    2023-08-23 05:25:02
  • Struts 2中实现Ajax的三种方式

    2022-04-30 05:46:28
  • c# mutex互斥量的深入解析

    2022-03-13 02:38:42
  • JAVA中的Token 基于Token的身份验证实例

    2023-11-09 18:05:09
  • Android帧动画、补间动画、属性动画用法详解

    2023-02-06 15:02:47
  • asp之家 软件编程 m.aspxhome.com