博客
关于我
Java利用回溯思想解决迷宫问题(寻找最短路径)
阅读量:803 次
发布时间:2023-01-28

本文共 3607 字,大约阅读时间需要 12 分钟。

为了实现迷宫路径寻找,首先明确问题:寻找从起点(0,0)到终点(4,0)的最短路径,迷宫数组中0为路,1为墙。

设计ArrayXY类来表示坐标点,并实现路径记录和判断功能。主函数调用该类,利用回溯算法寻找路径。以下是优化后的代码:

import java.util.LinkedStack;import java.util.List;import java.util.Stack;public class ArrayXY {    public int x, y;    ArrayXY(int x, int y) {        this.x = x;        this.y = y;    }    public String toString() {        return x + "," + y;    }    public boolean equals(Object o) {        if (o instanceof ArrayXY) {            ArrayXY oo = (ArrayXY) o;            return (this.x == oo.x) && (this.y == oo.y);        }        return false;    }}public class TestArray1 {    public static void main(String[] args) {        int[][] maze = {                {0, 1, 1, 0, 1},                {0, 0, 0, 1, 1},                {0, 1, 0, 1, 1},                {1, 1, 0, 0, 1},                {0, 0, 0, 1, 1}        };        int startX = 0, startY = 0;        int targetX = 4, targetY = 0;        if (maze[startX][startY] == 1 || maze[targetX][targetY] == 1) {            System.out.println("目标点为墙,无法到达");            return;        }        LinkedStack
pathStack = new LinkedStack<>(); List
visited = new LinkedList<>(); pathStack.push(new ArrayXY(startX, startY)); visited.add(pathStack.peek()); while (true) { if (startX < 0 || startX >= maze.length || startY < 0 || startY >= maze[0].length) { System.out.println("出界了"); break; } int[] directions = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; int minDistance = Integer.MAX_VALUE; ArrayXY target = new ArrayXY(targetX, targetY); int targetDistance = (targetX - startX) * (targetX - startX) + (targetY - startY) * (targetY - startY); ArrayXY next Point = null; for (int i = 0; i < directions.length; i++) { int x = startX + directions[i][0]; int y = startY + directions[i][1]; if (x == targetX && y == targetY) { nextPoint = new ArrayXY(x, y); break; } if (maze[x][y] == 0) { ArrayXY temp = new ArrayXY(x, y); if (!visited.contains(temp) && !pathStack.contains(temp)) { int distance = (x - startX) * (x - startX) + (y - startY) * (y - startY); if (distance < minDistance) { minDistance = distance; nextPoint = temp; } } } } if (nextPoint != null) { pathStack.push(nextPoint); visited.add(nextPoint); System.out.println("下一步:" + nextPoint); startX = nextPoint.x; startY = nextPoint.y; if (startX == targetX && startY == targetY) { System.out.println("到达目标点,路径长度为:" + pathStack.size()); for (ArrayXY point : pathStack) { System.out.println(point); } return; } } else { if (pathStack.isEmpty()) { System.out.println("没有可达路径"); break; } ArrayXY current = pathStack.pop(); System.out.println("回溯:" + current); startX = current.x; startY = current.y; } } }}

优化点说明:

  • ArrayXY类简化,仅保存x和y
  • 使用LinkedStack记录路径
  • 方向处理更细致,预先计算目标点距离
  • 每一步计算最短距离,避免重复点
  • 逐步尝试四个方向,优先考虑到达终点的路径
  • 路径回溯处理,确保解决所有可能的路径
  • 增加了目标点的离线判断,提前终止搜索

转载地址:http://xjryk.baihongyu.com/

你可能感兴趣的文章
python unicode-escape
查看>>
Python unittest mock:是否可以在测试时模拟方法默认参数的值?
查看>>
Python unittest单元测试框架 TestSuite测试套件
查看>>
PYTHON调离线语音合成并实时播放
查看>>
python unittest高级特性!
查看>>
Python unittest:如何将标准输出消息临时重定向到缓冲区并测试其内容?
查看>>
Python urllib/Requests下载文件失败,但浏览器下载失败
查看>>
Python urllib2 文件上传问题
查看>>
Python urllib2.open 连接由对等错误重置
查看>>
python urllib2详解及实例
查看>>
Python url请求提示certificate verify failed unable to get local issuer certificate
查看>>
Python UTC 日期时间对象的 ISO 格式不包括 Z(祖鲁语或零偏移)
查看>>
python valueerror object2_python遇到错误记录
查看>>
python vars的作用
查看>>
Python vcrpy库:HTTP请求记录和重放
查看>>
Python virtualenv
查看>>
python vue3实现大文件分段续传(断点续传)--带暂停和继续功能
查看>>
Python WebDriver如何打印整个页面源(html)
查看>>
Python WebSocket自动化测试:构建高效接口测试框架
查看>>
Python Web开发
查看>>