DFS和BFS例题

Posted zhihaospace

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DFS和BFS例题相关的知识,希望对你有一定的参考价值。

DFS和BFS例题

  • 机器人的运动范围:https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof/ ,使用bfs和dfs

import java.util.*;

public class P面试题13JiQiRenDeYunDongFanWeiLcof {
    //输入:m = 2, n = 3, k = 1
    //输出:3
    public static void main(String[] args) {
        Solution solution = new P面试题13JiQiRenDeYunDongFanWeiLcof().new Solution();
        // TO TEST
        System.out.println(solution.movingCount(2, 3, 1));
        System.out.println(solution.movingCount1(2, 3, 1));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int movingCount(int m, int n, int k) {

            boolean[][] visited = new boolean[m][n];

            int count = dfs(m, n, visited, 0, 0, k);

            return count;
        }

        public int movingCount1(int m, int n, int k) {

            boolean[][] visited = new boolean[m][n];

            LinkedList<int[]> queue = new LinkedList<>();
            queue.push(new int[]{0, 0});

            int count = 0;
            while (!queue.isEmpty()) {
                int size = queue.size();
                for (int i = size; i > 0; i--) {

                    int[] value = queue.poll();
                    int row = value[0];
                    int col = value[1];
                    if (check(m, n, visited, row, col, k)) {
                        visited[row][col] = true;
                        count++;
                        queue.push(new int[]{row + 1, col});
                        queue.push(new int[]{row, col + 1});
                        queue.push(new int[]{row - 1, col});
                        queue.push(new int[]{row, col - 1});
                    }
                }
            }

            return count;
        }


        private int dfs(int m, int n, boolean[][] visited, int row, int col, int k) {

            int count = 0;

            if (check(m, n, visited, row, col, k)) {

                visited[row][col] = true;
                count = 1 + dfs(m, n, visited, row + 1, col, k) +
                        dfs(m, n, visited, row, col + 1, k) +
                        dfs(m, n, visited, row - 1, col, k) +
                        dfs(m, n, visited, row, col - 1, k);
            }

            return count;
        }

        private boolean check(int m, int n, boolean[][] visited, int row, int col, int k) {

            if (row >= 0 && col >= 0 && row < m && col < n && !visited[row][col] && getSum(row, col, k)) {
                return true;
            }

            return false;
        }

        private boolean getSum(int row, int col, int k) {

            int sum = 0;
            while (row > 0) {
                sum += (row % 10);
                row /= 10;
            }
            while (col > 0) {
                sum += (col % 10);
                col /= 10;
            }

            if (sum <= k) {
                return true;
            }

            return false;
        }
    }
}
  • 矩阵中的路径:https://leetcode-cn.com/problems/ju-zhen-zhong-de-lu-jing-lcof/

public class P面试题12JuZhenZhongDeLuJingLcof{
    public static void main(String[] args) {
        Solution solution = new P面试题12JuZhenZhongDeLuJingLcof().new Solution();
        // TO TEST
    }
    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public boolean exist(char[][] board, String word) {
            int n = board.length;
            int m = board[0].length;

            boolean[][] visit = new boolean[n][m];

            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (hasPath(board, visit, word, i, j, 0)) {
                        return true;
                    }
                }
            }
            return false;
        }

        private boolean hasPath(char[][] board, boolean[][] visit, String word, int row, int col, int pathLength) {
            if (pathLength >= word.length()) {  //结束条件
                return true;
            }

            boolean has = false;
            if (row >= 0 && col >= 0 &&             //可行情况处理
                    row < board.length && 
                    col < board[0].length && 
                    board[row][col] == word.charAt(pathLength) && 
                    !visit[row][col]) {
                ++pathLength;

                visit[row][col] = true;     //处理标记

                has = hasPath(board, visit, word, row + 1, col, pathLength) ||
                        hasPath(board, visit, word, row, col + 1, pathLength) ||
                        hasPath(board, visit, word, row - 1, col, pathLength) ||
                        hasPath(board, visit, word, row, col - 1, pathLength);

                if (!has) {                 //还原标记
                    --pathLength;
                    visit[row][col] = false;
                }
            }

            return has;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}

 

以上是关于DFS和BFS例题的主要内容,如果未能解决你的问题,请参考以下文章

通过迷宫问题简单学习DFS和BFS算法

DFS和BFS例题

BFS/DFS处理二维矩阵抽象问题例题

深度优先dfs与广度bfs优先搜索总结+例题

精进DFS和BFS,按照流程来,秒杀

精进DFS和BFS,按照流程来,秒杀