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);
        
    }
}
相关推荐
努力努力再努力wz3 分钟前
【MySQL 进阶系列】拒绝滥用root:从 mysql.user 到权限校验,带你彻底理解用户管理与授权机制!
android·c语言·开发语言·数据结构·数据库·c++·mysql
生成论实验室32 分钟前
《源·觉·知·行·事·物:生成论视域下的统一认知语法》第五章 事:行在时空中的具体化
人工智能·算法·架构·知识图谱·创业创新
炸膛坦客41 分钟前
嵌入式 - 数据结构与算法:(1-4)数据结构 - 单链表的两个核心缺点(引入循环/双向链表)
c语言·数据结构·链表
Liangwei Lin42 分钟前
LeetCode 283. 移动零
算法
Lenyiin1 小时前
《LeetCode 顺序刷题》61 - 70
java·c++·python·算法·leetcode·lenyiin
岁岁的O泡奶1 小时前
NSSCTF_crypto_[LitCTF 2023]babyLCG
经验分享·python·算法·密码学·crypto·流密码
Hesionberger1 小时前
LeetCode 78:子集生成全攻略
java·开发语言·数据结构·python·算法·leetcode·职场和发展
前端之虎陈随易1 小时前
为什么今天还会有新语言?MoonBit 想解决什么问题?
大数据·linux·javascript·人工智能·算法·microsoft·typescript
risc1234561 小时前
DFA 的运行过程本身就是一种特殊的、空间优化的动态规划
算法·动态规划
仍然.1 小时前
算法题目---字符串
算法