当前位置: 首页 > ds >正文

Day50--图论--98. 所有可达路径(卡码网),797. 所有可能的路径

Day50–图论–98. 所有可达路径(卡码网),797. 所有可能的路径

刷今天的内容之前,要先去《代码随想录》网站,先看完:图论理论基础和深度优先搜索理论基础。做完之后可以看题解。有余力,把广度优先搜索理论基础也看了。

98. 所有可达路径(卡码网)

方法:回溯

思路:

本题的方法是回溯,具体思路都在代码注释中已有体现。

递归三部曲:

  1. 确定递归参数和返回值:private static void dfs(int node, int target)
  2. 确定终止条件:如果遍历到的node就是末尾,得到一条path,返回。if (node == target) res.add(new ArrayList(path));
  3. 递归中的处理操作:一个for循环,遍历当前node结点的邻接表nodeList。递归完,记得回溯!记得回溯!记得回溯!
import java.util.*;public class Main {// 类变量,不用传递参数private static List<List<Integer>> graph = new ArrayList<>();private static List<List<Integer>> res = new ArrayList<>();private static List<Integer> path = new ArrayList<>();public static void main(String[] args) {Scanner in = new Scanner(System.in);int n = in.nextInt();int m = in.nextInt();// 创建n+1个,方便下标for (int i = 0; i <= n; i++) {graph.add(new ArrayList<>());}// 输入边for (int i = 0; i < m; i++) {int from = in.nextInt();int to = in.nextInt();graph.get(from).add(to);}// 从1开始path.add(1);dfs(1, n);// 输出结果if (res.size() == 0) {System.out.println(-1);} else {print();}}private static void dfs(int node, int target) {// 如果该结点就是target,得到一条path,返回。if (node == target) {res.add(new ArrayList(path));return;}// 遍历这个node的邻接表nodeListList<Integer> nodeList = graph.get(node);for (int i : nodeList) {// path中加入元素path.add(i);// 下一层递归dfs(i, target);// 回溯:从path中移除元素path.remove(path.size() - 1);}}// 打印结果private static void print() {for (List<Integer> path : res) {// 先打第一个元素System.out.print(path.get(0));// 后面的元素都是空格+元素for (int i = 1; i < path.size(); i++) {System.out.print(" " + path.get(i));}// 打一个换行System.out.println("");}}
}

797. 所有可能的路径

思路:

和上一题是同一道题,不过不用自己处理输入和输出。

class Solution {List<List<Integer>> res = new ArrayList<>();List<Integer> path = new ArrayList<>();public List<List<Integer>> allPathsSourceTarget(int[][] graph) {int target = graph.length - 1;path.add(0);dfs(graph, 0, target);return res;}private void dfs(int[][] graph, int node, int target) {if (node == target) {res.add(new ArrayList(path));return;}for (int i = 0; i < graph[node].length; i++) {path.add(graph[node][i]);dfs(graph, graph[node][i], target);path.remove(path.size() - 1);}}
}
http://www.xdnf.cn/news/17783.html

相关文章:

  • 元宇宙虚拟金融服务全景解析:技术创新、场景重构与未来趋势
  • 一体化步进伺服电机在无人机舱门应用中的应用案例
  • 使用Gradle手搓一个Kotlin/Native项目
  • CMU-15445(9)——PROJECT#3-Query Execution-Task#2Task#3
  • 机器学习-决策树(上)
  • TDengine 可观测性最佳实践
  • Nginx反向代理功能
  • 微前端架构:原理、场景与实践案例
  • 扫雷 (minesweeper)
  • 从0-1搭建webpack的前端工程化项目
  • 【前端基础】15、列表元素、表格元素、表单元素(注:极其粗略的记载。)
  • (3万字详解)Linux系统学习:深入了解Linux系统开发工具
  • js异步操作 Promise :fetch API 带来的网络请求变革—仙盟创梦IDE
  • Java Web项目后台管理系统之内容管理仿写:内容、搜索、页码加载
  • Zabbix携手Grafana打造炫酷监控大屏
  • 【Linux文件操作】文件操作系统调用
  • 19.Linux DHCP服务
  • 2025.8.6 图论(1)Solution
  • MySQL 基本语法
  • 对自己的 app 进行分析, 诊断,审视
  • 多路转接 select
  • 常见鱼饵制作方式
  • FPGA学习笔记——DS18B20(数字温度传感器)
  • 如何解决pip安装报错ModuleNotFoundError: No module named ‘lightgbm’问题
  • 【C++】封装哈希表模拟实现unordered_set和unordered_map
  • 简单的身份验证中间件Tinyauth
  • 学习日志31 python
  • AI入门学习--如何写好prompt?
  • PyCharm(2025.1.3.1)绑定 Conda 环境
  • 类和对象(中上)