zoukankan      html  css  js  c++  java
  • [leetCode]463. 岛屿的周长

    题目

    链接:https://leetcode-cn.com/problems/island-perimeter

    给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地 0 表示水域。

    网格中的格子水平和垂直方向相连(对角线方向不相连)。整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表示陆地的格子相连组成的岛屿)。

    岛屿中没有“湖”(“湖” 指水域在岛屿内部且不和岛屿周围的水相连)。格子是边长为 1 的正方形。网格为长方形,且宽度和高度均不超过 100 。计算这个岛屿的周长。

    输入:
    [[0,1,0,0],
     [1,1,1,0],
     [0,1,0,0],
     [1,1,0,0]]
    
    输出: 16
    
    解释: 它的周长是下面图片中的 16 个黄色的边:
    

    在这里插入图片描述

    bfs

    思路: 遍历网格,当找到值为1的格子时进行广度优先搜索,使用计数器记录当前格子的个数,在遍历下一个格子之前将计数器的值进行累加然后重新开始计数,最后就能得到岛屿的周长。

    class Solution {
        public int islandPerimeter(int[][] grid) {
            int[][] dirs = new int[][]{{1,0},{0,1},{-1,0},{0,-1}};
            int rows = grid.length;
            int cols = grid[0].length;
            boolean[][] seen = new boolean[rows][cols];
            Queue<int[]> queue = new LinkedList<>();
            int ans = 0;
            for (int row = 0; row < rows; row++) {
                for (int col = 0; col < cols; col++) {
                    if (grid[row][col] == 1) {
                        queue.offer(new int[]{row, col});
                        seen[row][col] = true;
                        while (!queue.isEmpty()) {
                            int[] cur = queue.poll();
                            int cnt = 0;
                            for (int[] dir : dirs) {
                                int nx = cur[0] + dir[0];
                                int ny = cur[1] + dir[1];
                                if (nx < 0 || nx >= rows || ny < 0 
                                || ny >= cols || grid[nx][ny] == 0) {
                                    cnt++;
                                    continue;
                                }
                                if (seen[nx][ny]) continue;
                                queue.offer(new int[]{nx, ny});
                                seen[nx][ny] = true;
                            }
                            ans += cnt;
                        }
                        return ans;
                    }
                }
            }
            return ans;
        }
    }
    

    迭代

    思路: 遍历网格,如果格子值为1则对该格子的进行计数,然后累加,最后得到岛屿的周长

    class Solution {
        public int islandPerimeter(int[][] grid) {
            int[][] dirs = new int[][]{{1,0},{0,1},{-1,0},{0,-1}};
            int rows = grid.length;
            int cols = grid[0].length;
            int ans = 0;
            for (int row = 0; row < rows; row++) {
                for (int col = 0; col < cols; col++) {
                    int cnt = 0;
                    if (grid[row][col] == 1) {
                        for (int[] dir : dirs) {
                            int nx = row + dir[0];
                            int ny = col + dir[1];
                            if (nx < 0 || nx >= rows || ny < 0 
                            || ny >= cols || grid[nx][ny] == 0){
                                cnt++;
                            }
                        }
                    }
                    ans += cnt;
                }
            }
            return ans;
        }
    }
    

    dfs

    思路:遍历网格找到值为1的格子,然后进行深度优先搜索,将走过的格子值变为2,以防止死循环。每经过一个格子对其四个方向进行判断,并累加。

    class Solution {
    
        int[][] dirs = new int[][]{{1,0},{0,1},{-1,0},{0,-1}};
    
        public int islandPerimeter(int[][] grid) {
            
            int rows = grid.length;
            int cols = grid[0].length;
            for (int row = 0; row < rows; row++) {
                for (int col = 0; col < cols; col++) {
                   
                    if (grid[row][col] == 1) {
                        return dfs(grid, row, col, rows, cols);
                    }
                   
                }
            }
            return 0;
        }
    
        private int dfs(int[][] grid, int x, int y, int rows, int cols) {
            if (x < 0 || x >= rows || y < 0 || y >= cols || grid[x][y] == 0) {
                return 1;
            }
            if (grid[x][y] == 2) {
                return 0;
            }
            grid[x][y] = 2;
            int res = 0;
            for (int[] dir : dirs) {
                int nx = x + dir[0];
                int ny = y + dir[1];
                res += dfs(grid, nx, ny, rows, cols);
            }
            return res;
        }
    }
    
  • 相关阅读:
    RStudio 的使用
    R 语言文件读写
    R 语言文件读写
    matlab 编程的细节问题
    matlab 编程的细节问题
    matlab 图像分块及恢复
    matlab 图像分块及恢复
    matlab 高级函数 —— colfilt/blockproc (图像)矩阵的分块处理
    结构体/类中的弹性数组---元素个数为0的数组
    28.uva 10891 Game of Sum 记忆化dp
  • 原文地址:https://www.cnblogs.com/PythonFCG/p/13900728.html
Copyright © 2011-2022 走看看