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);
    }
}
相关推荐
毕竟是shy哥2 小时前
计算YOLO数据集中每个类的目标数
算法·yolo·机器学习
M78佐菲2 小时前
Linux学习笔记:TCP协议
linux·笔记·学习·tcp/ip·算法
学长毕业设计2 小时前
基于SpringBoot的公益基金管理系统(源码+文档+讲解视频)
java·spring boot·后端
东小西2 小时前
【SAA实战】第 3 篇 · 工具调用全攻略:把业务能力交给 Agent 自己调度
java·后端·spring
东小西2 小时前
【SAA实战】第 4 篇 · Agent 短期记忆:saver 让 Agent 跨轮记得住(threadId 隔离)
java·后端·spring
许彰午2 小时前
22-DataCenter报文序列化
java·低代码·架构·状态模式
2601_962065252 小时前
[MySQL] SQL优化之性能分析
java·sql·mysql
小范同学_3 小时前
JDK1.7 与 JDK1.8 HashMap 底层原理对比 + 数组并发扩容死循环详解
java·开发语言
晊晌_h3 小时前
嵌入式从0到精通——数据结构总结[特殊字符]
数据结构·算法·排序算法
我找到地球的支点啦4 小时前
Matlab系列(009) 一CRC循环冗余校验详解
开发语言·数据结构·算法·matlab·信息与通信