day52 图论part4

文章目录

  • [卡码网 110. 字符串迁移](#卡码网 110. 字符串迁移)
  • [卡码网 105. 有向图的完全联通](#卡码网 105. 有向图的完全联通)
  • [卡码网 106. 海岸线计算](#卡码网 106. 海岸线计算)

卡码网 110. 字符串迁移

注意如何连接连个字符串,然后使用bfs找到一条最短路径。

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

class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String beginStr = sc.next();
        String endStr = sc.next();
        Queue<String> q = new LinkedList<>();
        q.add(beginStr);
        sc.nextLine();
        Set<String> strset = new HashSet<>();
        Map<String, Integer> strmap = new HashMap<>();
        strmap.put(beginStr, 1);
        for (int i = 0; i < n; i++) {
            strset.add(sc.next());
        }
        while(!q.isEmpty()) {
            String word = q.poll();
            for (int i = 0; i < word.length(); i++) {
                char[] newword = word.toCharArray();
                for (char j = 'a'; j <= 'z'; j++) {
                    newword[i] = j;
                    String newString = new String(newword);
                    if (newString.equals(endStr)) {
                        System.out.println(strmap.get(word) + 1);
                        return;
                    }
                    if (strset.contains(newString) && !strmap.containsKey(newString)) {

                        strmap.put(newString, strmap.get(word) + 1);
                        q.add(newString);
                    }
                }
            }
        }
        System.out.println(0);

    }
}

卡码网 105. 有向图的完全联通

使用dfs遍历整个图,遍历到的点使用boolean数组标记。

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

class Main{

    public static void dfs(List<List<Integer>> graph, int node, boolean[] visited) {
        if (visited[node] == true) {
            return;
        }
        visited[node] = true;
        List<Integer> nodes = graph.get(node);
        for (int nextnode : nodes) {
            dfs(graph, nextnode, visited);
        }
    }
    public static void main(String[] agrs) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        List<List<Integer>> graph = new ArrayList<>();
        for (int i = 0; i <= n; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < k; i++) {
            int s = sc.nextInt();
            int t = sc.nextInt();
            graph.get(s).add(t);
        }
        boolean[] visited = new boolean[n + 1];
        dfs(graph, 1, visited);
        for (int i = 1; i <= n; i++) {
            if (visited[i] == false) {
            System.out.println(-1);
            return;
        }
        }
        System.out.println(1);
    }
}

卡码网 106. 海岸线计算

检查每一个陆地块的周围是否是边缘或者是水,是的话周长就加1。

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

class Main {
    static int count;
    static int[][] dir = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
    public static void helper(int[][] graph, int x, int y) {
        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 || graph[nextx][nexty] == 0) {
                count++;
            }
        }
    }
    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 res = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                count = 0;
                if (graph[i][j] == 1) {
                    helper(graph, i, j);
                    res += count;
                }
            }
        }
        System.out.println(res);
    }
}
相关推荐
H_BB5 分钟前
DFS实现回溯算法
数据结构·c++·算法·深度优先
汀、人工智能7 分钟前
[特殊字符] 第17课:滑动窗口最大值
数据结构·算法·数据库架构·图论·bfs·滑动窗口最大值
XiYang-DING16 分钟前
【LeetCode】232. 用栈实现队列
算法·leetcode·职场和发展
人道领域17 分钟前
【LeetCode刷题日记】142.环形链表Ⅱ
算法·leetcode·链表
2301_8227032026 分钟前
开源鸿蒙跨平台Flutter开发:基因序列比对基础:Needleman-Wunsch 算法的 Dart 实现
算法·flutter·开源·鸿蒙
Book思议-27 分钟前
【数据结构】「树」专题:树、森林与二叉树遍历之间的关系+408真题
数据结构·算法·二叉树··森林
Fcy64837 分钟前
算法基础详解(4)双指针算法
开发语言·算法·双指针
zk_ken37 分钟前
优化图像拼接算法思路
算法
xwz小王子39 分钟前
Nature Communications从结构到功能:基于Kresling折纸的多模态微型机器人设计
人工智能·算法·机器人
luj_176839 分钟前
从R语言想起的,。。。
服务器·c语言·开发语言·经验分享·算法