Day53:图论 岛屿数量 岛屿的最大面积

  1. 岛屿数量

时间限制:1.000S 空间限制:256MB

题目描述

给定一个由 1(陆地)和 0(水)组成的矩阵,你需要计算岛屿的数量。岛屿由水平方向或垂直方向上相邻的陆地连接而成,并且四周都是水域。你可以假设矩阵外均被水包围。

输入描述

第一行包含两个整数 N, M,表示矩阵的行数和列数。

后续 N 行,每行包含 M 个数字,数字为 1 或者 0。

输出描述

输出一个整数,表示岛屿的数量。如果不存在岛屿,则输出 0。

输入示例
复制代码
4 5
1 1 0 0 0
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1
输出示例
复制代码
3
提示信息

根据测试案例中所展示,岛屿数量共有 3 个,所以输出 3。

数据范围:

1 <= N, M <= 50

思路:

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

也就是说斜角度链接是不算了

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

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

dfs:

复制代码
import java.util.*;

class Main{
    
    public static void main(String[] args){
        int n,m;
        Scanner scanner = new Scanner(System.in);
        n=scanner.nextInt();
        m=scanner.nextInt();
        int[][] map=new int[n][m];
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                map[i][j]=scanner.nextInt();
            }
        }
        int result=0;
        boolean[][] visited=new boolean[n][m];
        for(int i=0;i<n;i++){
           for( int j=0;j<m;j++){
                if((!visited[i][j])&&map[i][j]==1){
                    result++;
                    visited[i][j]=true;
                    dfs(visited,map,i,j);
                }
            }
        }
      System.out.println(result);
    }
    public static void dfs(boolean[][] visited,int[][] map,int x,int y){
          int[][] dir={{0,1},{1,0},{-1,0},{0,-1}};
        for(int i=0;i<4;i++){
            int newx=x+dir[i][0];
            int newy=y+dir[i][1];
            if(newx>=0&&newx<map.length&&newy>=0&&newy<map[x].length&&!visited[newx][newy]&&map[newx][newy]==1){
                visited[newx][newy]=true;
                dfs(visited,map,newx,newy);
            }
        }
    }
}

BFS:

注意这里为了避免超时,加入队列就标记为访问过,避免结点的重复加入

复制代码
import java.util.*;

class Main{
    
    public static void main(String[] args){
        int n,m;
        Scanner scanner = new Scanner(System.in);
        n=scanner.nextInt();
        m=scanner.nextInt();
        int[][] map=new int[n][m];
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                map[i][j]=scanner.nextInt();
            }
        }
        int result=0;
        boolean[][] visited=new boolean[n][m];
        for(int i=0;i<n;i++){
           for( int j=0;j<m;j++){
                if((!visited[i][j])&&map[i][j]==1){
                    result++;
                    visited[i][j]=true;
                    bfs(visited,map,i,j);
                }
            }
        }
      System.out.println(result);
    }
   public static void bfs(boolean[][] visited, int[][] map, int x, int y) {
        int[][] dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
        Queue<int[]> queue = new LinkedList();
        queue.offer(new int[]{x, y});
        visited[x][y] = true;
        while (!queue.isEmpty()) {
            int[] poll = queue.poll();
            int curx = poll[0];
            int cury = poll[1];
            for (int i=0;i<4;i++){
                int newx=curx+dir[i][0];
                int newy=cury+dir[i][1];
                if(newx>=0&&newx<map.length&&newy>=0&&newy<map[x].length&&!visited[newx][newy]&&map[newx][newy]==1){
                   queue.add(new int[]{newx,newy});
                    visited[newx][newy]=true;
                }
            }
        }


    }
}

  1. 岛屿的最大面积

时间限制:1.000S 空间限制:256MB

题目描述

给定一个由 1(陆地)和 0(水)组成的矩阵,计算岛屿的最大面积。岛屿面积的计算方式为组成岛屿的陆地的总数。岛屿由水平方向或垂直方向上相邻的陆地连接而成,并且四周都是水域。你可以假设矩阵外均被水包围。

输入描述

第一行包含两个整数 N, M,表示矩阵的行数和列数。后续 N 行,每行包含 M 个数字,数字为 1 或者 0,表示岛屿的单元格。

输出描述

输出一个整数,表示岛屿的最大面积。如果不存在岛屿,则输出 0。

输入示例
复制代码
4 5
1 1 0 0 0
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1
输出示例
复制代码
4
提示信息

样例输入中,岛屿的最大面积为 4。

数据范围:

1 <= M, N <= 50。

思路:本题与上题一样,就是多了求每个岛屿面积的步骤

复制代码
import java.util.*;

class Main {

    public static void main(String[] args) {
        int n, m;
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        m = scanner.nextInt();
        int[][] map = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                map[i][j] = scanner.nextInt();
            }
        }
        int result = 0;
        boolean[][] visited = new boolean[n][m];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if ((!visited[i][j]) && map[i][j] == 1) {
                
                    visited[i][j] = true;
                  int s=  dfs(visited, map, i, j);
                  result=Math.max(result,s);
                    

                }
            }
        }
        System.out.println(result);
    }

    public static int dfs(boolean[][] visited, int[][] map, int x, int y) {
        int[][] dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
        int s=0;
        Queue<int[]> queue = new LinkedList();
        queue.offer(new int[]{x, y});
        s++;
        visited[x][y] = true;
        while (!queue.isEmpty()) {
            int[] poll = queue.poll();
            int curx = poll[0];
            int cury = poll[1];
            for (int i=0;i<4;i++){
                int newx=curx+dir[i][0];
                int newy=cury+dir[i][1];
                if(newx>=0&&newx<map.length&&newy>=0&&newy<map[x].length&&!visited[newx][newy]&&map[newx][newy]==1){
                   queue.add(new int[]{newx,newy});
                   s++;
                    visited[newx][newy]=true;
                }
            }
        }
return s;

    }
}
相关推荐
zheyutao5 小时前
字符串哈希
算法
A尘埃5 小时前
保险公司车险理赔欺诈检测(随机森林)
算法·随机森林·机器学习
大江东去浪淘尽千古风流人物6 小时前
【VLN】VLN(Vision-and-Language Navigation视觉语言导航)算法本质,范式难点及解决方向(1)
人工智能·python·算法
独好紫罗兰6 小时前
对python的再认识-基于数据结构进行-a003-列表-排序
开发语言·数据结构·python
wuhen_n7 小时前
JavaScript内置数据结构
开发语言·前端·javascript·数据结构
努力学算法的蒟蒻7 小时前
day79(2.7)——leetcode面试经典150
算法·leetcode·职场和发展
2401_841495647 小时前
【LeetCode刷题】二叉树的层序遍历
数据结构·python·算法·leetcode·二叉树··队列
AC赳赳老秦7 小时前
2026国产算力新周期:DeepSeek实战适配英伟达H200,引领大模型训练效率跃升
大数据·前端·人工智能·算法·tidb·memcache·deepseek
独好紫罗兰7 小时前
对python的再认识-基于数据结构进行-a002-列表-列表推导式
开发语言·数据结构·python
2401_841495647 小时前
【LeetCode刷题】二叉树的直径
数据结构·python·算法·leetcode·二叉树··递归