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);
    }
}
相关推荐
一只数据集6 分钟前
柏林道路路面图像数据集-971张沥青与鹅卵石路面图片-训练测试集划分-支持道路材质识别与自动驾驶视觉算法训练
算法·自动驾驶·材质
我不是懒洋洋7 分钟前
【数据结构】二叉树OJ(单值二叉树、检查两棵树是否相同、对称二叉树、二叉树的前序遍历、另一颗树的子树)
c语言·数据结构·c++·经验分享·算法·leetcode·visual studio
京师20万禁军教头8 分钟前
35面向对象(中级)-编程思想
java
yuzhiboyouye9 分钟前
java redis(缓存)
java·redis·缓存
wljy110 分钟前
每日一题(2026.4.29) 猫猫与数学
c语言·c++·算法·蓝桥杯·stl·牛客
sali-tec13 分钟前
C# 基于OpenCv的视觉工作流-章56-OCR
图像处理·人工智能·opencv·算法·计算机视觉·ocr
MicroTech202517 分钟前
微算法科技(NASDAQ:MLGO)混合经典量子算法:赋能数字图像处理的创新路径
科技·算法·量子计算
大大杰哥18 分钟前
DAG 学习笔记:从拓扑排序到并行执行
java
yu859395819 分钟前
降低OFDM系统PAPR的各种算法及误码率分析
前端·算法
2501_9130613419 分钟前
JVM虚拟机——面试中的八股文(下)
java·jvm·面试