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);
    }
}
相关推荐
San813_LDD1 小时前
[C语言]《Dev-C++ 报错解决手册(Day0607 精华版)》
java·前端·javascript
Anastasiozzzz2 小时前
从有限状态机到智能体图:传统 FSM 与 Agent Graph的演进
java·人工智能·python·ai
小欣加油8 小时前
leetcode56 合并区间
c++·算法·leetcode·职场和发展
lqqjuly8 小时前
前沿算法深度解析(二)
人工智能·算法·机器学习
wang09078 小时前
自己动手写一个spring之IOC_2
java·后端·spring
来杯@Java8 小时前
学生选课管理系统(基于springboot+vue前后端分离的项目)计算机毕业设计java
java·spring boot·spring·vue·毕业设计·maven·mybatis
徐小夕9 小时前
万字长文!千万级文档 RAG 知识库系统落地实践
前端·算法·github
不知名的老吴9 小时前
线程的生命周期之线程“插队“
java·开发语言·python
akunkuntaimei9 小时前
2026年高考数学各省真题及答案(完整版)
算法·高考