搜索(二)

这篇博客介绍了深度优先搜索(DFS)和广度优先搜索(BFS)在多个问题中的应用,包括130题的被包围区域问题、200题的岛屿数量计算、690题的员工重要性评估、429题的N叉树层序遍历以及994题的腐烂橘子扩散问题。通过这些实例展示了DFS和BFS在解决网格、树形结构和图结构问题上的有效性。
摘要由CSDN通过智能技术生成

130.被围绕的区域

class Solution130 {
    int[][] nextP = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

    public void dfs(char[][] board, int row, int col, int x, int y) {
        board[x][y] = 'A';
        for (int i = 0; i < 4; i++) {
            int nextX = x + nextP[i][0];BFS
            int nextY = y + nextP[i][1];

            if (nextX < 0 || nextY < 0 || nextX >= row || nextY >= col)
                continue;
            if (board[nextX][nextY] == 'O')
                dfs(board, row, col, nextX, nextY);
        }
    }

    public void solve(char[][] board) {
        int row = board.length;
        int col = board[0].length;

        for (int i = 0; i < col; i++) {

            if (board[0][i] == 'O')
                dfs(board, row, col, 0, i);
            if (board[row - 1][i] == 'O')
                dfs(board, row, col, row - 1, i);

        }
        for (int j = 1; j < row - 1; j++) {

            if (board[j][0] == 'O')
                dfs(board, row, col, j, 0);
            if (board[j][col - 1] == 'O')
                dfs(board, row, col, j, col - 1);

        }

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (board[i][j] == 'O') {
                    board[i][j] = 'X';
                } else if (board[i][j] == 'A') {
                    board[i][j] = 'O';
                } else {
                    continue;
                }
            }
        }
    }
}

200.岛屿数量

class Solution200 {
    int[][] nextP = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

    public void dfs(char[][] grid, int row, int col, boolean[][] visited, int x, int y) {
        visited[x][y] = true;
        for (int i = 0; i < 4; i++) {
            int nextX = x + nextP[i][0];
            int nextY = y + nextP[i][1];

            if (nextX < 0 || nextY < 0 || nextX >= row || nextY >= col)
                continue;
            if (grid[nextX][nextY] == '1' && !visited[nextX][nextY]) {
                dfs(grid, row, col, visited, nextX, nextY);
            }

        }
    }

    public int numIslands(char[][] grid) {

        int res = 0;
        int row = grid.length;
        int col = grid[0].length;
        boolean[][] visited = new boolean[row][col];

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (grid[i][j] == '1' && !visited[i][j]) {
                    res++;
                    dfs(grid, row, col, visited, i, j);
                }
            }
        }
        return res;
    }
}

690.员工的重要性

class Solution690 {
    //bfs
    public int getImportance(List<Employee> employees, int id) {
        HashMap<Integer, Employee> hashMap = new HashMap<>();

        for (int i = 0; i < employees.size(); i++) {
            hashMap.put(employees.get(i).id, employees.get(i));
        }
        Queue<Integer> queue = new LinkedList<>();
        queue.offer(id);

        int res = 0;
        while (!queue.isEmpty()) {
            int curId = queue.poll();
            res += hashMap.get(curId).importance;

            int size = hashMap.get(curId).subordinates.size();
            for (int i = 0; i < size; i++) {
                queue.offer(hashMap.get(curId).subordinates.get(i));
            }
        }
        return res;
    }
}

429.N叉树的层序遍历

class Solution429 {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null)
            return res;
        Queue<Node> treeQ = new LinkedList<>();
        treeQ.offer(root);

        while (!treeQ.isEmpty()) {
            int size = treeQ.size();
            List<Integer> tmp = new ArrayList<>();

            for (int i = 0; i < size; i++) {
                Node cur = treeQ.poll();
                if (cur != null) {
                    tmp.add(cur.val);
                    int childSize = cur.children.size();
                    for (int j = 0; j < childSize; j++) {
                        treeQ.offer(cur.children.get(j));
                    }
                }
            }
            res.add(tmp);

        }
        return res;
    }
}

994.腐烂的橘子

class Solution994 {
    int[][] nextP = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

    public int orangesRotting(int[][] grid) {

        int row = grid.length;
        int col = grid[0].length;
        Queue<HashMap<Integer, Integer>> queue = new LinkedList<>();

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (grid[i][j] == 2) {
                    HashMap<Integer, Integer> hashMap = new HashMap<>();
                    hashMap.put(i, j);
                    queue.offer(hashMap);
                }
            }
        }

        int minTime = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            boolean flag = true;
            while (size-- > 0) {
                HashMap<Integer, Integer> tmp = queue.poll();
                Iterator iter = tmp.entrySet().iterator();
                Integer x = -1;
                Integer y = -1;
                while (iter.hasNext()) {
                    Map.Entry entry = (Map.Entry) iter.next();
                    x = (Integer) entry.getKey();
                    y = (Integer) entry.getValue();
                }
                for (int i = 0; i < 4; i++) {
                    int nextX = x + nextP[i][0];
                    int nextY = y + nextP[i][1];

                    if (nextX < 0 || nextY < 0 || nextX >= row || nextY >= col)
                        continue;

                    if (grid[nextX][nextY] == 1) {
                        flag = false;
                        grid[nextX][nextY] = 2;
                        HashMap<Integer, Integer> map = new HashMap<>();
                        map.put(nextX, nextY);
                        queue.offer(map);
                    }

                }
            }
            if (!flag)
                minTime++;
        }

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (grid[i][j] == 1)
                    return -1;
            }
        }
        return minTime;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值