岛屿相关题目(深度优先遍历dfs)

牛逼题解

力扣icon-default.png?t=L892https://leetcode-cn.com/problems/number-of-islands/solution/dao-yu-lei-wen-ti-de-tong-yong-jie-fa-dfs-bian-li-/200. 岛屿数量

class Solution {
    private  int res;
    public  int numIslands(char[][] grid) {
        //如果是海洋,就是0;如果是陆地就是1;如果是已经遍历过的陆地就是置为2
        //深度优先遍历,遍历到不能遍历为止,就完成一次深度遍历;
        //每完成一次深度遍历,置为2的网格就组成了一个大岛屿,说明可以连成一篇,已经置为2的网格不会参与到其他岛屿的构成中。
        //然后进入下一次for循环遍历,当遇到值为1的陆地时就继续进入一次深度遍历,路径上置为2的网格就组成了另外一个大岛屿。
        //依次类推,直接所有的网格都遍历完成
        res = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == '1') {//每完成1次深度遍历,路径上为2的就是生成的新岛屿
                    dfsGrid(grid, i, j);
                    res++;
                }
            }
        }
        return res;
    }

    private  void dfsGrid(char[][] grid, int row, int col) {
        if (row >= grid.length || col >= grid[0].length || row < 0 || col < 0) {//超出边界就退出遍历
            return;
        }

        if (grid[row][col] != '1') {//不是陆地也退出遍历
            return;
        }

        grid[row][col] = '2';//当是陆地的时候,设为2,同时进行上下左右4个方向的陆地寻找
        dfsGrid(grid, row - 1, col);//向上搜索为1的陆地
        dfsGrid(grid, row + 1, col);//向下搜索为1的陆地
        dfsGrid(grid, row, col - 1);//向左搜索为1的陆地
        dfsGrid(grid, row, col + 1);//向右搜索为1的陆地
    }
}

695. 岛屿的最大面积 

    public  int maxAreaOfIsland(int[][] grid) {
        int ans = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == 1) {//
                    int curSum = dfsGrid(grid, i, j);
                    ans = Math.max(ans, curSum);
                }
            }
        }
        return ans;
    }


    private  int dfsGrid(int[][] grid, int row, int col) {
        if (row >= grid.length || col >= grid[0].length || row < 0 || col < 0) {//超出边界就退出递归
            return 0;
        }

        if (grid[row][col] != 1) {//不是陆地也退出递归
            return 0;
        }

        grid[row][col] = 2;//当是陆地的时候,设为2,同时进行上下左右4个方向的陆地寻找
        return 1
                + dfsGrid(grid, row - 1, col)//向上搜索为1的陆地
                + dfsGrid(grid, row + 1, col)//向下搜索为1的陆地
                + dfsGrid(grid, row, col - 1)//向左搜索为1的陆地
                + dfsGrid(grid, row, col + 1);//向右搜索为1的陆地
    }

463. 岛屿的周长

 

public static int islandPerimeter(int[][] grid) {
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == 1) {
                    return dfs(grid, i, j);//题目已经说了,只有一个岛屿,如果是多个,则需要将每一次深度遍历获取的周长再加起来
                }
            }
        }
        return 0;

    }

    public static int dfs(int[][] grid, int row, int col) {
        if (row < 0 || row > grid.length - 1 || col < 0 || col > grid[0].length - 1) {//邻居是超出的边界,对周长贡献1
            return 1;
        }
        if (grid[row][col] == 0) {//邻居是海洋,周长贡献1
            return 1;
        }
        if (grid[row][col] == 2) {//已经遍历过的陆地,对周长没有贡献
            return 0;
        }
        grid[row][col] = 2;//讲原来的陆地1,置为2,标记为遍历过

        return dfs(grid, row - 1, col)
                + dfs(grid, row + 1, col)
                + dfs(grid, row, col - 1)
                + dfs(grid, row, col + 1);
    }

猜你喜欢

转载自blog.csdn.net/u013385018/article/details/120263160