day51 图论part3

文章目录

  • [卡码网 101. 孤岛的总面积](#卡码网 101. 孤岛的总面积)
  • [卡码网 102. 沉没孤岛](#卡码网 102. 沉没孤岛)
  • [卡码网 103. 高山流水](#卡码网 103. 高山流水)
  • [卡码网 104. 建造最大岛屿](#卡码网 104. 建造最大岛屿)

卡码网 101. 孤岛的总面积

可以从行和列的遍历开始,从边缘向中间遍历 。

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

class Main{
    static int res = 0;
    static int count = 0;
    static boolean flag = false; 
    public static void dfs(int[][] graph, boolean[][] visited, int x, int y) {
        if (visited[x][y] || graph[x][y] == 0) {
            return;
        }
        if (x == 0 || y == 0 || x == graph.length - 1 || y == graph[0].length - 1) {
            flag =true;
        }
        count++;
        visited[x][y] =true;
        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;
            }
            dfs(graph, visited, nextx, nexty);
            
        }
    }   
    public static void main(String[] agrs) {
        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];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (!visited[i][j] && graph[i][j] == 1) {
                    dfs(graph, visited, i, j);
                }
                if (flag == false) {
                    res += count;
                }
                count = 0;
                flag = false;
            }
        }
        System.out.println(res);
 
}
}

卡码网 102. 沉没孤岛

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

public class Main{

    public static void dfs(int[][] graph, int x, int y) {
        if (graph[x][y] == 0 || graph[x][y] == 2) {
            return;
        }
        graph[x][y] = 2;
        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;
            }
            dfs(graph, nextx, nexty);
        }

    }
    public static void main(String[] agrs) {
        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();
            }
        }
        for (int i = 0; i < n; i++) {
            if (graph[i][0] == 1) {
                dfs(graph, i, 0);
            }
            if (graph[i][m - 1] == 1) {
                dfs(graph, i, m - 1);
            }
        }
        for (int j = 0; j < m; j++) {
            if (graph[0][j] == 1) {
                dfs(graph, 0, j);
            }
            if (graph[n - 1][ j] == 1) {
                dfs(graph, n - 1, j);
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (graph[i][j] == 2) {
                    graph[i][j] = 1;
                }
                else if (graph[i][j] == 1) {
                    graph[i][j] = 0;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                System.out.print(graph[i][j] + " ");
            }
            System.out.println();
        }
    }
}

卡码网 103. 高山流水

从边界开始遍历,逆向思维。

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

public class Main{

    public static void dfs(int[][] graph,boolean[][] visited, int x, int y) {
        if (visited[x][y]) {
            return;
        }
        visited[x][y] = true;
        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 (graph[x][y] <= graph[nextx][nexty]) {
                dfs(graph, visited, nextx, nexty);
            }
            
        }

    }
    public static void main(String[] agrs) {
        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[][] firstborder = new boolean[n][m];
        boolean[][] secondborder = new boolean[n][m];
        for (int i = 0; i < n; i++) {
                dfs(graph, firstborder, i, 0);
                dfs(graph, secondborder, i, m - 1);
        }
        for (int j = 0; j < m; j++) {
                dfs(graph,firstborder, 0, j);
                dfs(graph, secondborder, n - 1, j);
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (firstborder[i][j] == true && secondborder[i][j] == true) {
                    System.out.print(i + " " + j);
                    System.out.println();
                }
            }
        }
        
    }
}

卡码网 104. 建造最大岛屿

先计算出所有岛屿的面积,用map存储,之后再把0的位置设置为1看周围面积变化。

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

public class Main{
    static int mark = 2;
    static int count = 0;
    static int[][] dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
    public static void dfs(int[][] graph,boolean[][] visited, int x, int y, int mark) {
        if (visited[x][y]) {
            return;
        }
        visited[x][y] = true;
        graph[x][y] = mark;
        count++;
        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 (graph[nextx][nexty] == 1) {
                dfs(graph, visited, nextx, nexty, mark);
            }
           
            
        }

    }
    public static void main(String[] agrs) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] graph = new int[n][m];
        HashMap<Integer, Integer> mapsize = new HashMap<>();
        HashSet<Integer> setsize = new HashSet<>(); 
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                graph[i][j] = sc.nextInt();
            }
        }
        int maxIsland = 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) {
                    count = 0;
                    dfs(graph, visited, i, j, mark);
                    mapsize.put(mark, count);
                    mark++;
                }
                maxIsland = Math.max(maxIsland, count);
            }
        }
        int res = maxIsland;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int size = 1;
                if (graph[i][j] == 0) {
                    setsize.clear();
                    for (int k = 0; k < 4; k++) {
                        int neari = i + dir[k][0];
                        int nearj = j + dir[k][1];
                        if (neari < 0 || nearj < 0 || neari >= graph.length || nearj >= graph[0].length) {
                            continue;
                        } 
                        Integer cur_key = graph[neari][nearj];
                        if (mapsize.containsKey(cur_key) && !setsize.contains(cur_key)) {
                            size += mapsize.get(cur_key);
                            setsize.add(cur_key);
                        }
                    }
                    res = Math.max(res, size);
                }
            }
        }
    System.out.println(res);
        
    }
}
相关推荐
AI浩2 小时前
UTPTrack:迈向简单统一的视觉跟踪令牌剪枝
算法·机器学习·剪枝
nananaij2 小时前
【LeetCode-02 最小偶倍数 python解法】
python·算法·leetcode
倾心琴心2 小时前
【agent辅助pcb routing coding学习】实践4 kicad pcb 核心类层次关系
算法·agent·pcb·eda·routing
im_AMBER2 小时前
Leetcode 139 最后一个单词的长度 | 找出字符串中第一个匹配项的下标
开发语言·算法·leetcode
Frostnova丶2 小时前
(6)LeetCode.42 接雨水
数据结构·算法·leetcode
像污秽一样2 小时前
算法设计与分析-习题4.4
数据结构·算法·排序算法·深度优先
x_xbx2 小时前
LeetCode:102. 二叉树的层序遍历
算法·leetcode
2401_889884662 小时前
嵌入式C++测试框架
开发语言·c++·算法
月明长歌2 小时前
【码道初阶-Hot100】LeetCode 128. 最长连续序列:从排序双指针扫描到 HashSet,一文讲透为什么 O(n) 解法要用哈希
算法·leetcode·哈希算法