代码随想录算法训练营第五十六天 | 图论理论基础、深搜理论基础、98. 所有可达路径、797. 所有可能的路径、广搜理论基础、复习

图论理论基础

文档讲解:https://www.programmercarl.com/kamacoder/%E5%9B%BE%E8%AE%BA%E7%90%86...

深搜理论基础

文档讲解:https://www.programmercarl.com/kamacoder/%E5%9B%BE%E8%AE%BA%E6%B7%B1...

98. 所有可达路径

题目链接:https://kamacoder.com/problempage.php?pid=1170

文档讲解:https://www.programmercarl.com/kamacoder/0098.%E6%89%80%E6%9C%89%E5%8F...

思路

1、可以使用邻接矩阵或邻接表来记录点和边的情况。

java 复制代码
// 邻接矩阵
graph = new int[n + 1][n + 1];
while (in.hasNextInt()){
    int i = in.nextInt();
    int j = in.nextInt();
    graph[i][j] = 1;
}
// 邻接表
List<List<Integer>> graph = new ArrayList<>(n + 1);
for (int i = 0; i <= n; i++) {
    graph.add(new ArrayList<>());
}
for (int i = 0; i < m; i++) {
    int s = scanner.nextInt();
    int t = scanner.nextInt();
    // 使用邻接表,表示 s -> t 是相连的
    graph.get(s).add(t);
}

2、确认递归函数及参数:参数中需要传入当前遍历到的节点以及节点个数。

java 复制代码
public static void dfs(int x, int n) {}

3、确定终止条件:当当前遍历节点等于终点节点时,终止遍历,并将path加进结果列表res中。

java 复制代码
if (x == n) {
    res.add(new ArrayList(path));
    return;
}

4、处理循环中的逻辑:目前搜索节点出发的路径,如果对应的邻接矩阵中值为1,说明可达,就继续深度遍历。遍历后回溯path中的节点。

java 复制代码
// 邻接矩阵
for (int i = 1; i <= n; i++) {
    if (graph[x][i] == 1) {
        path.add(i);
        dfs(i, n);
        path.remove(path.size() - 1);
    }
}
// 邻接表
for (int i : graph.get(x)) { // 找到 x 指向的节点
    path.add(i);
    dfs(graph, i, n);
    path.remove(path.size() - 1);
}

代码

邻接矩阵+ACM模式

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

class Main{
    static List<List<Integer>> res = new ArrayList<>();
    static List<Integer> path = new ArrayList<>();
    static int[][] graph;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        graph = new int[n + 1][n + 1];
        while (in.hasNextInt()){
            int i = in.nextInt();
            int j = in.nextInt();
            graph[i][j] = 1;
        }
        path.add(1);
        dfs(1, n);
        if (res.size() == 0) System.out.println(-1);
        else {
            for (List<Integer> pa : res) {
                for (int i = 0; i < pa.size() - 1; i++) {
                    System.out.print(pa.get(i) + " ");
                }
                System.out.println(pa.get(pa.size() - 1));
            }
        }
    }
    public static void dfs(int x, int n) { // x为当前遍历的节点
        if (x == n) {
            res.add(new ArrayList(path));
            return;
        }
        
        for (int i = 1; i <= n; i++) {
            if (graph[x][i] == 1) {
                path.add(i);
                dfs(i, n);
                path.remove(path.size() - 1);
            }
        }
    }
}

797. 所有可能的路径

思路

和上一题的思路一样,只是这道题使用的是邻接表+核心代码模式。

代码

java 复制代码
class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        path.add(0);
        dfs(graph, 0);
        return res;
    }

    public void dfs(int[][] graph, int x) {
        if (x == graph.length - 1) {
            res.add(new ArrayList(path));
            return;
        }
        for (int i = 0; i < graph[x].length; i++) {
            path.add(graph[x][i]);
            dfs(graph, graph[x][i]);
            path.remove(path.size() - 1);
        }
    }
}

广搜理论基础

文档讲解:https://www.programmercarl.com/kamacoder/%E5%9B%BE%E8%AE%BA%E5%B9%BF...

复习栈与队列部分

150. 逆波兰表达式求值
239. 滑动窗口最大值
347.前 K 个高频元素
栈与队列总结

相关推荐
不知天地为何吴女士1 小时前
Day32| 509. 斐波那契数、70. 爬楼梯、746. 使用最小花费爬楼梯
算法
小坏坏的大世界1 小时前
C++ STL常用容器总结(vector, deque, list, map, set)
c++·算法
我命由我123452 小时前
Kotlin 数据容器 - List(List 概述、创建 List、List 核心特性、List 元素访问、List 遍历)
java·开发语言·jvm·windows·java-ee·kotlin·list
励志要当大牛的小白菜4 小时前
ART配对软件使用
开发语言·c++·qt·算法
qq_513970444 小时前
力扣 hot100 Day56
算法·leetcode
武子康4 小时前
Java-80 深入浅出 RPC Dubbo 动态服务降级:从雪崩防护到配置中心秒级生效
java·分布式·后端·spring·微服务·rpc·dubbo
PAK向日葵5 小时前
【算法导论】如何攻克一道Hard难度的LeetCode题?以「寻找两个正序数组的中位数」为例
c++·算法·面试
爱装代码的小瓶子6 小时前
数据结构之队列(C语言)
c语言·开发语言·数据结构
爱喝矿泉水的猛男7 小时前
非定长滑动窗口(持续更新)
算法·leetcode·职场和发展
YuTaoShao7 小时前
【LeetCode 热题 100】131. 分割回文串——回溯
java·算法·leetcode·深度优先