美文网首页
迷宫题的BFS和DFS解法

迷宫题的BFS和DFS解法

作者: 不懂zhuang懂的年岁 | 来源:发表于2016-09-06 09:29 被阅读744次

BFS

关键在于队列的使用,访问完当前节点再去访问孙子节点

DFS

可以用递归或者栈实现

package nowcoder.com;

import java.util.*;

/**
 * Created by Administrator on 2016/8/26.
 */
public class Maze {
    static int[] moveRow = {0, 1, 0, -1};
    static int[] moveCol = {1, 0, -1, 0};//用0和1代表4个方向,类似于枚举类

    static class MazeNode {//建立内部类,记录路径
        public int row;
        public int col;
        public MazeNode pre;

        public MazeNode(int row, int col) {
            this.row = row;
            this.col = col;
        }

        @Override
        public String toString() {//直接重写toString方法,print即可
            return "(" + row + "," + col + ")";
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int ROW = scanner.nextInt();
            int COL = scanner.nextInt();
            int[][] maze = new int[ROW][COL];//迷宫初始化
            for (int i = 0; i < ROW; i++) {
                for (int j = 0; j < COL; j++) {
                    maze[i][j] = scanner.nextInt();
                }
            }
            MazeNode start = new MazeNode(0, 0);
            ArrayList<MazeNode> result = solution(maze,start);//路径
//            ArrayList<MazeNode> result = solution1(maze, start);
//            for (int i = result.size() - 1; i >= 0; i--) {
//                System.out.println(result.get(i));
//            }

            for (int i = 0; i < result.size(); i++) {
                System.out.println(result.get(i));

            }
        }
    }

    private static ArrayList<MazeNode> solution1(int[][] maze, MazeNode start) {//深度遍历
        ArrayList<MazeNode> list = new ArrayList<>();
        MazeNode node = new MazeNode(0, 0);
        int row = maze.length;
        int col = maze[0].length;
        boolean mark[][] = new boolean[row][col];
        BFS(maze, start, mark, list, node);
        ArrayList<MazeNode> result = new ArrayList<>();
        while (node.row != 0 || node.col != 0) {
            result.add(node);
            node = node.pre;
        }
        result.add(new MazeNode(0, 0));
        return result;
    }

    private static void BFS(int[][] maze, MazeNode start, boolean[][] mark, ArrayList<MazeNode> list, MazeNode end) {
        Queue<MazeNode> queue = new LinkedList<>();
        queue.add(start);//BFS用队列
        while (!queue.isEmpty()) {
            MazeNode node = queue.poll();//取出队首的节点
            list.add(node);
            if (node.row == maze.length - 1 && node.col == maze[0].length - 1) {
                end.col = node.col;
                end.row = node.row;
                end.pre = node.pre;
                return;
            }
            int i = node.row;
            int j = node.col;
            mark[i][j] = true;//访问标记

            for (int k = 0; k < 4; k++) {//for循环,把节点r的临界点都访问完才去访问孙子节点
                int tmpi = i + moveRow[k];
                int tmpj = j + moveCol[k];
                if (tmpi >= 0 && tmpi < maze.length && tmpj >= 0 && tmpj < maze[0].length && maze[tmpi][tmpj] == 0 && !mark[tmpi][tmpj]) {
                    MazeNode node1 = new MazeNode(tmpi, tmpj);
                    node1.pre = node;
                    list.add(node1);
                    queue.add(node1);
                }
            }
        }
    }


    private static ArrayList<MazeNode> solution(int[][] maze, MazeNode start) {
        int row = maze.length;
        int col = maze[0].length;
        boolean mark[][] = new boolean[row][col];
        ArrayList<MazeNode> path = new ArrayList<>();
        ArrayList<MazeNode> result = new ArrayList<>();
        DFS(maze, start, mark, path, result);
        return result;
    }

    private static void DFS(int[][] maze, MazeNode start, boolean[][] mark, ArrayList<MazeNode> path, ArrayList<MazeNode> result) {

        if (start.col == maze[0].length - 1 && start.row == maze.length - 1) {
            path.add(new MazeNode(maze.length - 1, maze[0].length - 1));
            int size = path.size();
            for (int i = 0; i < size; i++) {
                result.add(path.get(i));
            }
        }
        int i = start.row, j = start.col;
        mark[start.row][start.col] = true;
        path.add(start);
        for (int k = 0; k < 4; k++) {
            int tmpi = i + moveRow[k];
            int tmpj = j + moveCol[k];
            if (tmpi >= 0 && tmpi < maze.length && tmpj >= 0 && tmpj < maze[0].length && maze[tmpi][tmpj] == 0 && !mark[tmpi][tmpj]) {
                MazeNode node1 = new MazeNode(tmpi, tmpj);
               //或者用非递归的while(!path.isEmpty())  path.add(node1);
                DFS(maze, node1, mark, new ArrayList<>(path), result);//递归实现DFS
            }
        }
    }
}



相关文章

  • 迷宫题的BFS和DFS解法

    BFS 关键在于队列的使用,访问完当前节点再去访问孙子节点 DFS 可以用递归或者栈实现

  • 矩阵搜索、图相关算法整理

    dfs ,求连通块等 dfs ,指定路径搜索 BFS求迷宫距离

  • LeetCode Invert Binary Tree(镜像)

    二叉树的镜像。 解法一(dfs recursive): 解法二(bfs queue): 注:解法二是层序遍历bfs...

  • LeetCode 130. 被围绕的区域(dfs,bfs,并查集

    这道题有三种解法,可以用dfs或者bfs或者并查集来做使用bfs或者dfs思路:开一个二维布尔数组,记录哪些区域被...

  • DFS与BFS LeetCode 刷题小结(一)

    本节我们将汇总一些 LeetCode bfs与dfs相关的题。 关于深度优先搜索(DFS)和广度优先搜索(BFS)...

  • [LeetCode] 107. Binary Tree Leve

    原题 层序遍历,从自底向上按层输出。 左→右→中解法一 :DFS,求出自顶向下的,最后返回时反转一下。 解法二:BFS

  • leetcode79 单词搜索

    题目 单词搜索 分析 解法:dfs没啥可讲的,dfs初级练习题目(当然bfs也没问题)。 代码

  • LeetCode 力扣 102. 二叉树的层次遍历

    题目描述(中等难度) 二叉树的层次遍历,输出一个 list 的 list。 解法一 DFS 这道题考的就是 BFS...

  • 拓扑排序

    拓扑排序是面试常考的题之一,O(m+n)的复杂度,m为边数,n为顶点数解法:BFS,DFS两种解法。用途:解决DA...

  • 朋友圈

    解法一:深度优先遍历DFS(递归) 解法二:广度优先遍历BFS(队列) 解法三:并查集 使用了一种合适的数据结构,...

网友评论

      本文标题:迷宫题的BFS和DFS解法

      本文链接:https://www.haomeiwen.com/subject/olirettx.html