Bfs--迷宫问题1.0

广度优先遍历实现迷宫问题1.0

 

maze.java

import java.util.Scanner;
import java.util.Random;
public class maze {
    //地图的大小
    //
    static int n = 8;
    //
    static int m = 7;
    //用来保存地图
    static int[][] map = new int[n][m];
    //用来保存是否被访问过
    static int[][] isVisited = new int[n][m];
    //记录所有可行路径终点前一个下标
    static int[] train = new int[n * m];
    //遵循 左下右上 的顺序
    static int[][] path = {{0, -1},
                            {1, 0},
                            {0, 1},
                            {-1, 0}};
    //利用数组节点存放遍历内容
    static Node[] nodes = new Node[n * m];
    //判断是否已经到达终点
    static boolean flag = false;
    static boolean f = false;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //使用1表示为墙,上下置为1
        for (int i = 0; i < m; i++) {
            map[0][i] = 1;
            map[n-1][i] = 1;
        }
        //左右置为1
        for (int i = 0; i < n; i++) {
            map[i][0] = 1;
            map[i][m-1] = 1;
        }

//        n=6;m=5
//        map[2][2]=1;
//        map[3][2]=1;
//        map[4][2]=1;
//        map[5][2]=1;

        //        //设置挡板
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n - 3; j = j + 3) {
                Random rand = new Random();
                int num = rand.nextInt(m-1);
                if(num == 0){ num++; }
                map[i][num] = 1;
            }
        }
        //输出地图
        System.out.println("地图的情况");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                System.out.print(map[i][j] + " ");
            }
            System.out.println();
        }
        //输入判断操作
        while (!f) {
            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();
        //判断要查询的起点和终点坐标 是否符合标准
            if (!isStandard(start_x, start_y) ) {
                System.out.println("输入的起点坐标有误~~~");
            }
            else if(!isStandard(end_x, end_y)){
                System.out.println("输入的终点坐标有误~~~");
            }else if (start_x == end_x && start_y == end_y){
                System.out.println("起点终点不能相同~~~");
            }else{
                bfs(start_x, start_y, end_x, end_y);
                return;
            }
        }

    }


    //传进来起点坐标和终点坐标
    public static void bfs(int x, int y, int end_x, int end_y) {
        //当前第几个元素
        int first = 0;
        int index = 0;
        int count = 0;

        isVisited[x][y] = 1;
        nodes[first] = new Node(x, y, -1);
        int next_x;
        int next_y;
        try {
            while (!flag) {
                for (int i = 0; i < path.length; i++) {
                    //开始四个方向分别遍历
                    next_x = nodes[first].x + path[i][0];
                    next_y = nodes[first].y + path[i][1];
                    //直到遍历的对象的上一个是终点时退出遍历
                    if (nodes[index].x == end_x && nodes[index].y == end_y) {
                        flag = true;
                    }
                    //判断是否符合标准,将其存储到nodes[]中,first为它的前一个
                    if (isStandard(next_x, next_y)) {
                        isVisited[next_x][next_y] = 1;
                        nodes[++index] = new Node(next_x, next_y, first);
//                        System.out.print(index +"   "+first+"  "+ nodes[index].pre+"      ");
                    }
                    //如果到达终点,将其first保存到train[]中
                    if (next_x == end_x && next_y == end_y) {
                        train[++count] = first;
                        //为了找到所有能到达终点的路径
                        isVisited[next_x][next_y] = 0;
                        break;
                   }
                }
                first++;
//               System.out.println(first+"---------");
            }
        } catch (Exception e) {
                System.out.println("此路无法到达终点!");
                return;
        }

//          for(int i = 0; i <=index;i++){
//              System.out.println("[" + nodes[i].x + "," + nodes[i].y + "]");
//          }
        //判断路径个数,打印
        if (count!=0) {
            System.out.println("走过的所有路径坐标为:");
            for (int i = 1; i <= count; i++) {
                //打印走过的路径,用first递归遍历到达它的每个路径
                printPath(train[i]);
                System.out.println("[" + end_x + "," + end_y + "]");
            }
        }

    }
    //判断这个点是否符合标准
    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 printPath(int index) {
        if (nodes[index].pre == -1) {
            System.out.print("[" + nodes[index].x + "," + nodes[index].y + "]-->");
        } else {
            printPath(nodes[index].pre);
            System.out.print("[" + nodes[index].x + "," + nodes[index].y + "]-->");
        }
    }
}

 

Node.java

public 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;
    }
}

 

posted @ 2021-12-08 20:27  夜布多  阅读(57)  评论(0编辑  收藏  举报