Day51 >> 99、计数孤岛 + 100、最大岛屿面积

代码随想录-图论Part2

99、计数孤岛

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

public class Main {
    public static int[][] dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
    public static void dfs(boolean[][] visited, int x, int y, int[][] grid) {
        for (int i = 0; i < 4; i++) {
            int nextX = x + dir[i][0];
            int nextY = y + dir[i][1];
            if (nextY < 0 || nextX < 0|| nextX >= grid.length || nextY >= grid[0].length) {
                continue;
            }
            if (!visited[nextX][nextY] && grid[nextX][nextY] == 1) {
                visited[nextX][nextY] = true;
                dfs(visited, nextX, nextY, grid);
            }
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int m = scanner.nextInt();
        int n = scanner.nextInt();
        int[][] grid = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                grid[i][j] = scanner.nextInt();
            }
        }
        boolean[][] visited = new boolean[m][n];
        int ans = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) { 
                if (!visited[i][j] && grid[i][j] == 1) {
                    ans++;
                    visited[i][j] = true;
                    dfs(visited, i, j, grid);
                }
            }
        }
        System.out.println(ans);
        scanner.close();
    }
}

100、最大岛屿面积

java 复制代码
package test.java;

import java.util.Scanner;

public class dfsPart2 {
    static final int[][] dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
    static int result = 0;
    static int count = 0;
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int 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();
            }
        }
        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) {
                    count = 0;
                    dfs(visited, i, j, map);
                    result = Math.max(result, count);
                }
            }
        }
        System.out.println(result);
        scanner.close();
    }

    public static void dfs(boolean[][] visited, int x, int y, int[][] grid) {
        count++;
        visited[x][y] = true;
        for (int i = 0; i < 4; i++) { 
            int newX = x + dir[i][0];
            int newY = y + dir[i][1];
            if (newX < 0 || newY < 0 
                || newX >= grid.length || newY >= grid[0].length
                || visited[newX][newY] || grid[newX][newY] == 0) {
                continue;
            }
            dfs(visited, newX, newY, grid);
        }
    }
}
相关推荐
mounter6252 小时前
【硬核前沿】CXL 深度解析:重塑数据中心架构的“高速公路”,Linux 内核如何应对挑战?-- CXL 协议详解与 LSF/MM 最新动态
linux·服务器·网络·架构·kernel
camellias_3 小时前
【无标题】
java·tomcat
咸鱼2.03 小时前
【java入门到放弃】需要背诵
java·开发语言
zzzyyy5383 小时前
Linux环境变量
linux·运维·服务器
椰猫子3 小时前
Java:异常(exception)
java·开发语言
kebeiovo4 小时前
atomic原子操作实现无锁队列
服务器·c++
win x4 小时前
Redis 使用~如何在Java中连接使用redis
java·数据库·redis
星晨雪海4 小时前
基于 @Resource 的支付 Service 多实现类完整示例
java·开发语言
CHHC18804 小时前
NetCore树莓派桌面应用程序
linux·运维·服务器
阿维的博客日记4 小时前
什么是逃逸分析
java·juc