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);
    }
}
相关推荐
ch.ju4 小时前
Java程序设计(第3版)第二章——java的数据类型:小数
java
无限进步_4 小时前
【C++】巧用静态变量与构造函数:一种非常规的求和实现
开发语言·c++·git·算法·leetcode·github·visual studio
Advancer-4 小时前
RedisTemplate 两种序列化实践方案
java·开发语言·redis
小超超爱学习99374 小时前
大数乘法,超级简单模板
开发语言·c++·算法
java1234_小锋4 小时前
Java高频面试题:MyBatis如何实现动态数据源切换?
java·开发语言·mybatis
墨神谕4 小时前
Java中,为什么要将.java文件编译成,class文件,而不是直接将.java编译成机器码
java·开发语言
Ricardo-Yang4 小时前
SCNP语义分割边缘logits策略
数据结构·人工智能·python·深度学习·算法
凌波粒4 小时前
LeetCode--344.反转字符串(字符串/双指针法)
算法·leetcode·职场和发展
啊哦呃咦唔鱼4 小时前
LeetCode hot100-543 二叉树的直径
算法·leetcode·职场和发展
Nyarlathotep01134 小时前
并行设计模式(3):Future模式
java·后端