day 65 图论part02 99.岛屿数量 深搜 99.岛屿数量 广搜 100.岛屿的最大面积

99.岛屿数量 深搜

每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。

本题思路,是用遇到一个没有遍历过的节点陆地,计数器就加一,然后把该节点陆地所能遍历到的陆地都标记上。

在遇到标记过的陆地节点和海洋节点的时候直接跳过。 这样计数器就是最终岛屿的数量。

那么如何把节点陆地所能遍历到的陆地都标记上呢,就可以使用 DFS,BFS或者并查集。

java 复制代码
import java.util.*;

public class Main {
    static int[][] dirs = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    static int ans = 0;
    static boolean[][] visited;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] graph = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                graph[i][j] = sc.nextInt();
            }
        }
        visited = new boolean[n][m];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if(!visited[i][j] && graph[i][j] == 1){
                    bfs(i,j,graph,n,m);
                    ans++;
                }
            }
        }
        System.out.println(ans);

    }

    private static void bfs(int row, int col, int[][] graph, int n, int m) {
        if (visited[row][col]){
            return;
        }
        visited[row][col] = true;
        for (int[] dir : dirs) {
            int nextR = dir[0] + row;
            int nextC = dir[1] + col;
            if (nextR >= 0 && nextR < n && nextC >= 0 && nextC < m){
                if (graph[nextR][nextC] == 1){
                    bfs(nextR,nextC,graph,n,m);
                }
            }
        }
    }
}

广搜

java 复制代码
class Solution {
    public int numIslands(char[][] grid) {
        int count = 0;
        for(int i = 0; i < grid.length; i++) {
            for(int j = 0; j < grid[0].length; j++) {
                if(grid[i][j] == '1'){
                    bfs(grid, i, j);
                    count++;
                }
            }
        }
        return count;
    }
    private void bfs(char[][] grid, int i, int j){
        Queue<int[]> list = new LinkedList<>();
        list.add(new int[] { i, j });
        while(!list.isEmpty()){
            int[] cur = list.remove();
            i = cur[0]; j = cur[1];
            if(0 <= i && i < grid.length && 0 <= j && j < grid[0].length && grid[i][j] == '1') {
                grid[i][j] = '0';
                list.add(new int[] { i + 1, j });
                list.add(new int[] { i - 1, j });
                list.add(new int[] { i, j + 1 });
                list.add(new int[] { i, j - 1 });
            }
        }
    }
}

100.岛屿的最大面积

java 复制代码
public class Main {
    static int[][] dirs = {{1,0},{-1,0},{0,1},{0,-1}};
    static boolean[][] visited;
    static int ans = Integer.MIN_VALUE;
    static int temp;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] graph = new int[n][m];
        visited = new boolean[n][m];
        // 初始化
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                graph[i][j] = sc.nextInt();
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (!visited[i][j] && graph[i][j] == 1){
                    temp = 0;
                    bfs(i, j, n, m, graph);
                    ans = Math.max(ans, temp);
                }
            }
        }
        System.out.printf("%d", ans == Integer.MIN_VALUE ? 0:ans);

    }

    private static void dfs(int row, int col, int n, int m, int[][] graph) {
        if (visited[row][col]){
            return;
        }
        visited[row][col] = true;
        temp++;
        for (int[] dir : dirs) {
            int nextR = row + dir[0];
            int nextC = col + dir[1];
            if(nextR >= 0 && nextR < n && nextC >= 0 && nextC < m && graph[nextR][nextC] == 1){
                bfs(nextR,nextC,n,m,graph);
            }
        }
    }


}
java 复制代码
class Solution {
    public int maxAreaOfIsland(int[][] grid) {
        int res = 0;
        for(int i = 0; i < grid.length; i++){
            for(int j = 0; j < grid[0].length; j++){
                if(grid[i][j] == 1){
                    res = Math.max(res, bfs(grid, i, j));
                }   
            }
        }
        return res;
    }

    public int bfs(int[][] grid, int i, int j){
        int[] dx = {1, -1, 0, 0};
        int[] dy = {0, 0, 1, -1};
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[]{i, j});
        grid[i][j] = 0;
        int area = 1;
        while(!queue.isEmpty()){
            int[] x = queue.poll();
            for(int index = 0; index < 4; index++){
                int nx = x[0] + dx[index], ny = x[1] + dy[index];
                if(nx>=0 && nx < grid.length && ny >= 0 && ny < grid[0].length && grid[nx][ny] == 1){
                    grid[nx][ny] = 0;
                    area += 1;
                    queue.offer(new int[]{nx, ny});
                }
            }
        }
        return area;
    }
}
相关推荐
Puppet__3 分钟前
【康复学习--LeetCode每日一题】3115. 质数的最大距离
学习·算法·leetcode
dc爱傲雪和技术9 分钟前
卡尔曼滤波Q和R怎么调
python·算法·r语言
Neituijunsir25 分钟前
2024.06.28 校招 实习 内推 面经
c++·python·算法·面试·自动驾驶·汽车·求职招聘
xw-pp34 分钟前
回溯法的小结与概述
java·数据结构·c++·python·算法·递归
每天努力进步!1 小时前
LeetCode热题100刷题6:160. 相交链表、206. 反转链表、234. 回文链表、141. 环形链表、142. 环形链表 II
c++·算法·leetcode·链表
weixin_432702761 小时前
统计信号处理基础 习题解答11-1
算法·信号处理
爱编程的Tom1 小时前
Map && Set(Java篇详解)
java·开发语言·数据结构·学习·算法
JokerSZ.3 小时前
【Leetcode 每日一题】268. 丢失的数字
数据结构·算法·leetcode
㣲信团队3 小时前
小和问题和逆序对问题
数据结构·算法