代码地址以下:
http://www.demodashi.com/demo/14547.htmlhtml
使用深度优先算法求解迷宫路径,使用Java实现求解过程的可视化,可单步运行,形象直观。java
红色格子为迷宫终点,迷宫可放大缩小,为了录屏选择了较小的尺寸,有多种不一样难度的迷宫能够加载。node
文件中有两个运行脚本,Windows下直接双击win运行.bat
便可,linux和Mac运行sh文件中的命令便可,喜欢用IDE的也可自行建立项目。
运行项目后,点击菜单栏左上角的Map
加载迷宫地图, 点击右下角的Run
开始解迷宫,Step
可单步运行,可经过速度进度条调节速度。linux
Maze ├── classes # 存放编译生成的class文件 ├── lib.jar # 打包好的gui库 ├── map # 迷宫地图文件 │ ├── EasyMaze.txt │ ├── FinalMaze01.txt │ ├── FinalMaze02.txt │ ├── FinalMaze03.txt │ ├── FinalMaze04.txt │ └── FinalMaze05.txt ├── src │ ├── MazeBug.java │ └── MazeBugRunner.java ├── linux运行.sh # 运行脚本 └── win运行.bat # 运行脚本
使用深度优先算法
,每一个格子下一步都有上下左右4种走法,可是这4种走法并非都是合法的,好比有些格子有障碍物,有些格式在边界以外,去掉这些剩下的才是合法的走法。算法
深度优先算法的思想就是:ide
算法优化
:上面的方法选择下一步走的方向是随机的,或者按照上下左右的顺序选择。可是不少迷宫都有偏向性,好比若是有右偏向性,那么每次都优先往右走能够更快走出迷宫。因此在实现的时候,记录每一个方向走的次数,每往一个方向走一步就加1,若是回退就该方向减1,每次走都优先走次数最多的方向,当迷宫有偏向性时,该方法效率更高。优化
以项目中的迷宫为例,大部分状况下偏向性所需步数更少。ui
普通方法: 534 1175 350 973 1052 偏向性: 552 761 330 175 420
/* * 节点:存储方向和该方向所走的次数 * 往一个方向前进则加1,后退则减1 */ class Node { private int dir; // 方向,角度值 private int ct; // 该方向所走次数 public Node(int initdir, int initct) { dir = initdir; ct = initct; } public int getDir() { return dir; } public int getCt() { return ct; } public void setCt(int deta) { ct += deta; } } // 深度优先算法解迷宫,而且以小甲虫的形式呈现 public class MazeBug extends Bug { private Location next; // 下一步要走的格子 private Integer stepCount = 0; // 所走的步数 private boolean isEnd = false; // 是否到达迷宫出口 private boolean hasShown = false; // 是否显示告终束信息 private Stack<Location> path = new Stack<>(); // 存储走过的路径 private ArrayList<Node> arr = new ArrayList<>(); public MazeBug() { setColor(Color.GREEN); arr.add(new Node(0, 0)); arr.add(new Node(90, 0)); arr.add(new Node(270, 0)); arr.add(new Node(180, 0)); } // 周期性执行 public void act() { boolean willMove = canMove(); // 是否还能继续移动 if (isEnd) { // 是否结束 if (!hasShown) { // 是否显示结束消息 String msg = stepCount.toString() + " steps"; JOptionPane.showMessageDialog(null, msg); hasShown = true; } return; } else if (willMove) { // 向前移动一个,步数加1 move(); ++stepCount; } else { // 不能移动,后退一步,将该方向的计数器减1 Grid<Actor> grid = getGrid(); Location loc = this.getLocation(); Location top = path.pop(); ++stepCount; grid.remove(top); this.setDirection(loc.getDirectionToward(top)); this.moveTo(top); // 在走过的死路留下一朵白花 Flower flower = new Flower(Color.WHITE); flower.putSelfInGrid(getGrid(), loc); // 方向计数器减1 int dir = 180 + ((getDirection() / 90) % 2) * 180 - getDirection(); for (Node node : arr) if (node.getDir() == dir) { node.setCt(-1); return; } } } // 找出和当前位置相邻的、合法的而且从未走过的格子 public Location getValid(Location loc) { Grid<Actor> gr = getGrid(); if (gr == null) return null; // 将每一个方向走过的次数从大到小排序,下一步优先选次数多的方向走 Location adjLocation; arr.sort(new Comparator<Node>() { @Override public int compare(Node a, Node b) { return (a.getCt() < b.getCt()) ? 1 : -1; } }); for (Node node : arr) { adjLocation = this.getLocation().getAdjacentLocation(node.getDir()); if (gr.isValid(adjLocation) && (gr.get(adjLocation) == null || gr.get(adjLocation).getColor().equals(Color.RED))) { node.setCt(1); return adjLocation; } } return null; } // 判断当前位置是否能够继续移动 public boolean canMove() { Grid<Actor> gr = getGrid(); Actor adj; Location loc = this.getValid(this.getLocation()); if (loc != null) { adj = gr.get(loc); next = loc; isEnd = adj != null && adj.getColor().equals(Color.RED); return true; } return false; } // 将甲虫的方向转向下一格,往前移动一步,将原来的位置压栈,并放置一朵绿花,表示走过的路径 public void move() { Grid<Actor> gr = getGrid(); if (gr == null) return; Location loc = this.getLocation(); path.push(loc); this.setDirection(loc.getDirectionToward(next)); this.moveTo(next); Flower flower = new Flower(this.getColor()); flower.putSelfInGrid(gr, loc); } }
跟算法无关的代码,好比GUI方面的都打包成lib.jar
了,若是想要本身更改能够自行解压。Java实现可视化迷宫this
代码地址以下:
http://www.demodashi.com/demo/14547.htmlcode
注:本文著做权归做者,由demo大师代发,拒绝转载,转载须要做者受权