迷宫问题 最短路径(java)实现

迷宫问题利用java的宽度优先搜索实现寻找最短路径

题目描述:

 int[][] map= { {0, 1, 0, 0, 0},
                {0, 0, 0, 1, 0},
                {1, 1, 0, 0, 0},
                {0, 0, 0, 0, 1},
                {0, 0, 0, 0, 0}};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

求最短路径问题:
不能直接用队列,需要用到数组模拟队列,去保存每个点的前一个点.

下面是具体的代码实现.


下面的代码可以实现从地图上任意起点开始,到指定终点的最短路径,代码的具体功能代码里面注释的很清楚了:

import java.util.Scanner;

public class Test {

    //地图的大小
    static int n = 5;
    static int m = 5;
    //用来保存地图
    static int[][] map;
    //用来保存是否被访问过
    static int[][] isVisited = new int[n][m];
    //遵循 左下右上 的顺序走
    static int[][] trval = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
    //利用数组模拟队列
    static Node[] nodes = new Node[n * m];
    //判断是否已经到达终点
    static boolean flag = false;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        map = new int[][]{
                {0, 1, 0, 0, 0},
                {0, 0, 0, 1, 0},
                {1, 1, 0, 0, 0},
                {0, 0, 0, 0, 1},
                {0, 0, 0, 0, 0}};
        System.out.print("请输入起点坐标:");
        int start_x = sc.nextInt();
        int start_y = sc.nextInt();
        System.out.print("请输入终点坐标:");
        int end_x = sc.nextInt();
        int end_y = sc.nextInt();

        bfs(start_x, start_y, end_x, end_y);


    }


    //传进来起点的x,y的起点和终点坐标
    static void bfs(int x, int y, int end_x, int end_y) {
        //判断要查询的起点和终点坐标 是否在地图上
        if (!isStandard(x, y) || !isStandard(end_x, end_y)) {
            System.out.println("输入的坐标有误~~~");
            return;
        }

        //当前第几个元素
        int head = 0;
        int index = 0;

        isVisited[x][y] = 1;
        nodes[head] = new Node(x, y, -1);
        int next_x;
        int next_y;
        while (!flag) {

            for (int i = 0; i < trval.length; ++i) {
                next_x = nodes[head].x + trval[i][0];
                next_y = nodes[head].y + trval[i][1];

                if (isStandard(next_x, next_y)) {
                    isVisited[next_x][next_y] = 1;
                    nodes[++index] = new Node(next_x, next_y, head);
                }
                if (next_x == end_x && next_y == end_y) {
                    flag = true;
                    break;
                }
            }
            head++;
        }

        //打印走过的路径
        System.out.println("走过的路径坐标为:");
        printTrval(--head);
        System.out.println("终点");

    }

    //判断这个点是否超出地图范围
    static boolean isStandard(int x, int y) {
        if (x < n && x >= 0 && y >= 0 && y < m && map[x][y] == 0 && isVisited[x][y] == 0) {
            return true;
        }
        return false;
    }

    //打印走过的路径
    static void printTrval(int index) {
        if (nodes[index].pre == -1) {
            System.out.print("[" + nodes[index].x + "," + nodes[index].y + "]-->");
        } else {
            printTrval(nodes[index].pre);
            System.out.print("[" + nodes[index].x + "," + nodes[index].y + "]-->");
        }
    }


}

//用来保存每个结点以及前一个结点
class Node {
    int x;
    int y;
    int pre;

    public Node(int x, int y, int pre) {
        this.x = x;
        this.y = y;
        this.pre = pre;
    }
}
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值