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

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.lryc.cn/news/618874.html

相关文章:

  • Javase 之 字符串String类
  • Python 多进程及进程间通信
  • C++实现LINGO模型处理程序
  • 杰里常用功能API
  • Navicat更改MySql表名后IDEA项目启动会找原来的表
  • 腾讯codebuddy.ai 安装实测【从零开始开发在线五子棋游戏:完整开发记录】
  • 服务降级方式
  • 2025年最新原创多目标算法:多目标酶作用优化算法(MOEAO)求解MaF1-MaF15及工程应用---盘式制动器设计,提供完整MATLAB代码
  • 拖动式看板工具TOP6:2025最新评测
  • 疯狂星期四文案网第37天运营日记
  • 看懂 Makefile 第一课:基础
  • 企业培训笔记:宠物信息管理--实现宠物信息的添加
  • c#,vb.net全局多线程锁,可以在任意模块或类中使用,但尽量用多个锁提高效率
  • 行业分享丨SimSolid 在汽车零部件开发中应用的可行性调研及实践
  • 基于Hadoop的汽车价格预测分析及评论情感分析可视化系统
  • 海信IP108H(53U1M)_S905L-B主控-无线SV6051P/8822CS(通刷咪咕mg100_mg101)线刷固件包
  • grpc浅入门
  • 一键生成 Android 适配不同分辨率尺寸的图片
  • 什么是 Spring MVC?
  • AuthController类讲解
  • 龙舌兰人造植物、Apple Watch保护壳、厨房水槽收纳架、家居磁性挂钩等亚马逊热销单品,正在外观专利TRO维权!
  • 备战国赛算法讲解——马尔科夫链,2025国赛数学建模B题详细思路模型更新
  • VUE+SPRINGBOOT从0-1打造前后端-前后台系统-会议记录
  • Linux网络--2.2、TCP接口
  • 5 重复匹配
  • 51 单片机分层架构的模块依赖关系图
  • 详细解释RBFT和NoxBFT及RAFT的差异
  • PCIe Electrical Idle Sequences ( EIOS and EIEOS )
  • Java 22 新特性:字符串模板(String Templates)让拼接更优雅、更安全
  • 机械学习--TF-IDF实战--红楼梦数据处理