代码随想录| 深搜、797.所有可能的路径、广搜、200. 岛屿数量

本文介绍了深度优先搜索(DFS)在图上的应用,以及广度优先搜索(BFS)在二叉树层序遍历中的运用,还提到了如何修正二叉树岛屿计数的代码实现,展示了搜索策略的递归和迭代过程。
摘要由CSDN通过智能技术生成

深度优先搜索

回溯算法其实就是深搜,只不过这里的深搜是侧重于在图上搜索,回溯大多是在树上搜索。

797.所有可能的路径

完成

代码

模板题

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    // 搜索以node为根的图
    public void dfs(int[][] graph, int node) { 
        if(node == graph.length-1){
            res.add(new ArrayList<>(path));
            return;
        }
        // 遍历和node直连的所有节点
        for(int index = 0; index < graph[node].length; index++){
            path.add(graph[node][index]);
            dfs(graph, graph[node][index]);
            path.removeLast();
        }
    }
    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        path.add(0);
        dfs(graph, 0);
        return res;
    }
}

广度优先搜索

二叉树的层序遍历其实就是广度优先搜索。广搜一般需要栈或队列的辅助,但不局限于栈和队列,数组也可以。

200. 岛屿数量

完成

思路

我的第一版代码在搜索过程中,只会搜索节点右边和下边的节点,这是不对的。应该搜索节点上下左右四个方向的节点,因为在搜索过程中,从哪个方向搜到该节点是未知的,比如"工"字形的岛屿,搜索时最下排的位置最先搜到中间的节点,此时要往两边扩散,而不是只管右边。

代码

class Solution {
    boolean[][] visited;
    int[][] move = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
    public int numIslands(char[][] grid) {
        visited = new boolean[grid.length][grid[0].length];
        int res = 0;
        // 找岛屿
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if(!visited[i][j]&&grid[i][j] == '1'){
                    bfs(grid, i, j);
                    res++;
                }
            }
        }
        return res;
    }
    // 广搜,把连通的陆地打上标记
    public void bfs(char[][] grid, int y, int x){
        Deque<int[]> queue = new LinkedList<>();
        queue.offer(new int[]{y, x});
        visited[y][x] = true;
        while (!queue.isEmpty()) {
            int[] node = queue.poll();
            for (int i = 0; i < 4; i++) {
                int nextx = node[1] + move[i][1];
                int nexty = node[0] + move[i][0];
                if(nextx < 0 || nexty >= grid.length || nexty < 0 || nextx >= grid[0].length) continue;
                if(!visited[nexty][nextx] && grid[nexty][nextx] == '1') {
                    queue.offer(new int[]{nexty, nextx}); 
                    visited[nexty][nextx] = true; //只要加入队列就标记为访问
                }
            }
        }
    }
}
  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值