当前位置: 首页 > 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.dtcms.com/a/328246.html

相关文章:

  • Quartz
  • Mybatis源码解读-SqlSession 会话源码和Executor SQL操作执行器源码
  • 谷歌云代理商:用 AI 启航,Gemini 重塑旅游酒店行业新体验
  • 【SpringBoot】07 容器功能 - SpringBoot底层注解的应用与实战 - @ConfigurationProperties配置绑定
  • 从0入门LangGraph,手搓高质量Agent
  • 【自动化运维神器Ansible】playbook文件内变量定义全流程解析
  • 谷歌ADK接入文件操作MCP
  • Linux中Https配置与私有CA部署指南
  • Java 工厂方法模式
  • C++单继承虚函数表探索
  • 京东方 DV133FHM-NN1 FHD13.3寸 工业液晶模组技术档案
  • 玩转Docker | 使用Docker部署Radicale日历和联系人工具
  • [激光原理与应用-250]:理论 - 几何光学 - 透镜成像的优缺点,以及如克服缺点
  • 万物平台模型导入样例大全(实时更新中~)
  • SM4对称加密算法的加密模式介绍
  • JavaEE 初阶第十八期:叩开网络世界的大门(上)
  • ffmpeg-AVFilter 和 Filter Graph 使用指南
  • ffmpeg,ffplay, vlc,rtsp-simple-server,推拉流命令使用方法,及测试(二)
  • Stereolabs ZED相机 选型指南:双目 / 单目、短距 / 长距,如何为机器人视觉系统匹配最优方案?
  • 力扣-394.字符串解码
  • 【模型剪枝2】不同剪枝方法实现对 yolov5n 剪枝测试及对比
  • Homebrew 入门教程(2025 年最新版)
  • 获取虚谷数据库所有表名、表注释、字段名、字段类型、字段注释到word中
  • clickhouse基础概念及集群部署
  • 疏老师-python训练营-Day43复习日
  • Qwen-Image(阿里通义千问)技术浅析(一)
  • 谷歌 Web Guide 如何重塑搜索排名及其 SEO 影响
  • python技巧:控制转台的2个坑。
  • 从关键词到智能决策:孟庆涛如何用GEO重塑AI时代的搜索优化范式
  • 2025年受自适应差分进化-无人机路径规划的统一元启发式框架-附Matlab完整代码