BFS-迷宫问题

来源:互联网 发布:gson解析复杂的json 编辑:程序博客网 时间:2024/04/29 21:44

问题描述

定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0,};它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

Input

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

Output

左上角到右下角的最短路径,格式如样例所示。

Sample Input

0 1 0 0 00 1 0 1 00 0 0 0 00 1 1 1 00 0 0 1 0

Sample Output

(0, 0)(1, 0)(2, 0)(2, 1)(2, 2)(2, 3)(2, 4)(3, 4)(4, 4)

代码

import java.util.LinkedList;import java.util.Queue;import java.util.Scanner;import java.util.Stack;public class Test16 {    // 用于记录当前节点    public static class Node {        int x;        int y;        int step;    }    static int[][] migong = new int[6][6];    // 记录防止重走    static boolean[][] vsd = new boolean[6][6];    // 记录上一步情况    static int[][] pre_step = new int[6][6];    // 用于层次遍历    static Queue<Node> queue = new LinkedList<>();    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        for (int i = 0; i < 5; i++) {            for (int j = 0; j < 5; j++) {                migong[i][j] = in.nextInt();                if (migong[i][j] == 1) {                    vsd[i][j] = true;                }            }        }        Node node = new Node();        node.x = 0;        node.y = 0;        node.step = 0;        bfs(node);    }    // 思路:使用bfs思想,走到最后一步migong[4][4],不断记录上一步位置,到达最后位置后,退回去回初始位置migong[0][0],就是最短路径。    // 用变量1,2,3,4分别记录为上下左右,放进数组中用来控制变量    // 用一个数组记录上一步情况    // 用一个stack利用先进后出,输出路径    public static boolean cheak(Node node) {        if (node.x >= 0 && node.x < 5 && node.y >= 0 && node.y < 5 && !vsd[node.x][node.y]) {            return true;        } else {            return false;        }    }    // 方向控制    static int[][] dir = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };    public static void bfs(Node node) {        // 使用两个node记录当前和下一个的状态        queue.offer(node);        Node now;        vsd[node.x][node.y] = true;        Node next = new Node();        while (!queue.isEmpty()) {            now = queue.peek();            // 控制方向 0右1左2下3上            for (int i = 0; i < 4; i++) {                next.x = now.x + dir[i][0];                next.y = now.y + dir[i][1];                if (cheak(next)) {                    vsd[next.x][next.y] = true;                    next.step = now.step + 1;                    // 此处注意 一定要用零时Node加入队列中,因为java中对象是引用传递                    Node tmp = new Node();                    tmp.x = next.x;                    tmp.y = next.y;                    tmp.step = now.step + 1;                    queue.offer(tmp);                    pre_step[next.x][next.y] = i;                }            }            queue.poll();            if (now.x == 4 && now.y == 4) {                System.out.println("最短路径为:" + now.step);                break;            }        }        DisPlay();    }    // 输出所走路径    public static void DisPlay() {        Stack<Node> stack = new Stack<>();        for (int i = 0; i < 5; i++) {            for (int j = 0; j < 5; j++) {                System.out.print(pre_step[i][j]);            }            System.out.println();        }        int i = 4, j = 4;        System.out.println("(" + i + "," + j + ")");        while (true) {            switch (pre_step[i][j]) {            case 0:                j = j - 1;                break;            case 1:                j = j + 1;                break;            case 2:                i = i - 1;                break;            case 3:                i = i + 1;                break;            default:                break;            }            System.out.println("(" + i + "," + j + ")");            if (i == 0 && j == 0) {                break;            }        }    }}

 

 

原创粉丝点击