广度优先遍历

什么时候需要分层遍历?

什么时候不用分层遍历?

有时候刷题看解答就会疑惑,为什么在广度优先遍历的时候有的题解答中类似以下代码:

while (!queue.isEmpty()) {
    int queueSize = queue.size();
    for (int i = 0; i < queueSize; i++) {
        int[] node = queue.poll();
        int curNodeValue = matrix[node[0]][node[1]];
        if (curValue == 0) {
            // 到达目的地
            return Cnt;
        }
        for (int[] moveStep : moveSteps) {
             int newRow = node[0] + moveStep[0];
             int newCol = node[1] + moveStep[1];
             if (newRow < 0 || newRow >= rowNum || newCol < 0 || newCol >= colNum) {
                 // 超过matrix范围了
                 continue;
             }
             int[] newNode = new int[]{newRow, newCol};
             if (matrix[newRow][newCol] == -1) {
                 // 遇到障碍物,跳过这个点
                 continue;
             }
             if (visited[newRow][newCol] == 0) {
                 // 还没有访问过的node
                 queue.offer(newNode);
                 visited[newRow][newCol] = 1;
             }
         }
    }
    // 为什么在这里++
    cnt++;
}

这是因为这里在做层次遍历,就是说辐射是一层一层括出去的,先遍历完这一层,再去下一层

不仅要一个一个遍历,在遍历的时候还要有同层关系,遍历完这层,才算完成一步,cnt才能++。

又比如计算岛屿数量这个题,因为最终只需要求整个矩阵里有多少岛屿,没有层次遍历,只需要一层while循环即可:

class Solution {
    private int count = 0;
    private int[][] moveSteps = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    public int numIslands(char[][] grid) {
        if (grid.length == 0 || grid[0].length == 0) {
            return 0;
        }
        int row = grid.length;
        int col = grid[0].length;

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (grid[i][j] == '1') {
                    count++;
                    grid[i][j] = '0';
                    bfs(grid, i, j);
                }
            }
        }
        return count;
    }

    private void bfs(char[][] grid, int row, int col) {
        Queue<int[]> queue = new LinkedList<>();
        queue.add(new int[]{row, col});
        while (!queue.isEmpty()) {
            int[] node = queue.poll();
            for (int[] moveStep : moveSteps) {
                int newRow = node[0] + moveStep[0];
                int newCol = node[1] + moveStep[1];
                if (newRow < 0 || newRow >= grid.length
                        || newCol < 0 || newCol >= grid[0].length) {
                    continue;
                }
                if (grid[newRow][newCol] == '1') {
                    queue.add(new int[]{newRow, newCol});
                    grid[newRow][newCol] = '0';
                }
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值