day 50 图论part2

文章目录

  • [卡码网 99 计数孤岛](#卡码网 99 计数孤岛)
  • [卡码网 99 计数孤岛](#卡码网 99 计数孤岛)
  • [卡码网 100 最大岛屿的面积](#卡码网 100 最大岛屿的面积)

卡码网 99 计数孤岛

dfs 深度搜索,注意终止条件的位置。

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

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

    }

    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();
            }
        } 

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

卡码网 99 计数孤岛

使用bfs。

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

public class Main {
    static class pair{
        int first;
        int second;
        pair(int first, int second) {
            this.first = first;
            this.second = second;
        }
    }
    public static void bfs(int[][] graph, boolean[][] visited, int x, int y) {
        Queue<pair> dq = new LinkedList<pair> ();
        visited[x][y] = true;
        dq.add(new pair(x, y));
        int[][] dir = {{1, 0}, {0, 1}, {0, -1}, {-1, 0}};
        while (!dq.isEmpty()) {
            int curx = dq.peek().first;
            int cury = dq.poll().second;
            for (int i = 0; i < 4; i++) {
                int nextx = curx + dir[i][0];
                int nexty = cury + dir[i][1];
                if (nextx < 0 || nextx >= graph.length || nexty < 0 || nexty >= graph[0].length) {
                    continue;
                }
                if (!visited[nextx][nexty] && graph[nextx][nexty] == 1) {
                    dq.add(new pair(nextx, nexty));
                    visited[nextx][nexty] = true;
                }
            }
        }

    }

    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();
            }
        } 

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

卡码网 100 最大岛屿的面积

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

class Main{
    static int res = 0;
    public static void dfs (int[][] graph, boolean[][] visited, int x, int y) {
        if (visited[x][y] || graph[x][y] == 0) {
            return;
        }
        visited[x][y] = true;
        res++;
        int[][] dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
        for (int i =0; i < 4; i++) {
            int nextx = x + dir[i][0];
            int nexty = y + dir[i][1];
            if (nextx < 0 || nexty < 0 || nextx >= graph.length || nexty >= graph[0].length) {
                continue;
            } 
            if (!visited[nextx][nexty] && graph[nextx][nexty] == 1) {
                dfs(graph, visited, nextx, nexty);
            }
        }
    }
    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();
            }
        }
        int end = 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] && graph[i][j] == 1) {
                    res = 0;
                    dfs(graph, visited, i, j);
                    end = Math.max(end, res);
                }
            }
        }
        System.out.println(end);
    }
}
相关推荐
小bo波15 小时前
Java Swing 图形用户界面实验 —— 从算术练习到游戏开发的完整实践
java·课程设计·gui·游戏开发·扫雷·swing
咖啡八杯17 小时前
GoF设计模式——备忘录模式
java·后端·spring·设计模式
HjhIron20 小时前
面试常客:字符串算法从入门到进阶
算法·面试
吴佳浩1 天前
DeepSeek DSpark:Confidence-Scheduled Speculative Decoding 技术解析
人工智能·算法·deepseek
触底反弹1 天前
🧠 搞懂 Token,才算真正入门大模型——从分词原理到 Embedding 语义实战
javascript·人工智能·算法
SamDeepThinking1 天前
裁掉那个差程序员后,给你看团队里高手的代码:这个习惯,希望你有
java·后端·程序员
vivo互联网技术1 天前
ICLR 2026 | 基于后验采样的图像恢复方法LearnIR:人脸去阴影、去雾
人工智能·算法·aigc
朕瞧着你甚好1 天前
技术雷达 & Java 集成评估报告 — Apache Tika 3.3.1
java·ai编程
浮生望1 天前
JS字符串与回文算法:从包装类到双指针的面试进阶之路
javascript·算法
黄敬峰1 天前
面试必刷:从JS底层包装类到双指针,彻底搞懂字符串与回文算法
算法