使用队列解决迷宫问题代码(java语言)

public class QueueMaze {
    public static void main(String[] args){
        int [][] maze = {{1,1,1,1,1,1},
                         {1,0,1,0,0,1},
                         {1,0,0,1,1,1},
                         {1,0,1,0,0,1},
                         {1,0,0,0,0,1},
                         {1,1,1,1,1,1}};
        QueueNode.getMazeResult(maze,1,1,4,4);
    }

}

class QueueNode { //进队的结点
    int x;// 左右位置
    int y;// 上下位置
    QueueNode previous;//前结点

    public QueueNode(int x, int y) {
        this.x = x;
        this.y = y;
        this.previous = null;
    }

    /** (这里提醒) int[y][x]数组中第一个位置是上下y位置,第二个位置才是左右x位置 防止混淆
     * @param maze   迷宫
     * @param startX 开始位置的x轴
     * @param startY 开始位置的y轴
     * @param endX   结束位置的x轴
     * @param endY   结束位置的y轴
     * @return 是否有路径
     */
    //进行
    public static boolean getMazeResult(int[][] maze, int startX, int startY, int endX, int endY) {
        Queue<QueueNode> queue = new LinkedList<>();//建造一个队列来存储进队结点
        QueueNode startNode = new QueueNode(startX, startY);//开始结点
        queue.offer(startNode);//先入队
        maze[startY][startX] = -1;//入队的位置进行填充,防止重复入队
        //进行循环 队不为空时
        QueueNode popNode = null;
        while (!queue.isEmpty()) {
            popNode = queue.poll();//出列一个结点
            //如果该出列的结点对应位置为结束结点,则直接展示,返回true,结束
            if (popNode.x == endX && popNode.y == endY) {
                showByEndNode(popNode);
                return true;
            }
            for (int i = 0; i < 4; i++) { //从4个方向扫描可以走的方块
                int insertX = 0;
                int insertY = 0;
                //四个方向的X和Y
                switch (i) {
                    case 0:
                        insertX = popNode.x - 1;
                        insertY = popNode.y;
                        break;
                    case 1:
                        insertX = popNode.x + 1;
                        insertY = popNode.y;
                        break;
                    case 2:
                        insertX = popNode.x;
                        insertY = popNode.y + 1;
                        break;
                    case 3:
                        insertX = popNode.x;
                        insertY = popNode.y - 1;
                        break;
                }
                //判断X和Y是否符合 insertX >= 0 && insertX < maze.length && insertY >= 0 && insertY < maze.length 是判断越界
                // maze[insertX][insertY] == 0 是判断是没有走过的路
                if (insertX >= 0 && insertX < maze.length && insertY >= 0 && insertY < maze.length
                        && maze[insertY][insertX] == 0) {
                    //使当前位置设置为已走过
                    maze[insertY][insertX] = -1;
                    //符合则建立新结点 并且前驱结点为当前出列结点
                    QueueNode node = new QueueNode(insertX, insertY);
                    node.previous = popNode;
                    queue.offer(node);//入队
                }
            }
        }
        //队列为空 说明没有路径 则返回false;
        return false;
    }

    //获取最终结果
    public static void showByEndNode(QueueNode endNode) {
        while (endNode != null) {
            System.out.printf("[%d,%d] ", endNode.y, endNode.x);
            endNode = endNode.previous;
        }
    }
}

内容仅供个人参考...

--2022-03-31

  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值