华为OD机试 - 两个字符串间的最短路径问题(Java & JS & Python & C & C++)

题目描述:

给定一个字符串图(其中每个字符代表一个节点,并且所有节点都连接到其他节点)和两个节点A和B,请找出A和B之间的最短路径。

解决方案:

这个问题可以通过广度优先搜索(BFS)算法来解决。我们从节点A开始,并且我们在每一步扩展所有相邻节点。我们将记录每个节点的父节点,以便在找到最短路径时回溯。

以下是使用不同编程语言实现的解决方案:

  1. Java:



public class ShortestPathBetweenTwoStrings {
    public String[] findShortestPath(String graph, String A, String B) {
        // 使用HashSet存储访问过的节点
        HashSet<Character> visited = new HashSet<>();
        // 使用LinkedList作为队列
        Queue<Pair> queue = new LinkedList<>();
        // 使用HashMap记录每个节点的父节点
        HashMap<Character, Character> parentMap = new HashMap<>();
 
        // 将起点加入队列
        queue.offer(new Pair(A.charAt(0), null));
        visited.add(A.charAt(0));
 
        // BFS搜索
        while (!queue.isEmpty()) {
            Pair current = queue.poll();
            char currentChar = current.node;
            char parent = current.parent;
 
            // 如果当前节点是B,则找到了最短路径
            if (currentChar == B.charAt(0)) {
                // 从B回溯到A构建最短路径
                Stack<Character> stack = new Stack<>();
                while (parent != null) {
                    stack.push(currentChar);
                    currentChar = parent;
                    parent = parentMap.get(currentChar);
                }
                stack.push(currentChar); // 加入起点A
                String[] path = new String[stack.size()];
                for (int i = 0; i < path.length; i++) {
                    path[i] = String.valueOf(stack.pop());
                }
                return path;
            }
 
            // 扩展所有未访问的邻居节点
            for (int i = 0; i < graph.length(); i++) {
                if (graph.charAt(i) == currentChar && !visited.contains(graph.charAt(i))) {
                    visited.add(graph.charAt(i));
                    queue.offer(new Pair(graph.charAt(i), currentChar));
                    parentMap.put(graph.charAt(i), currentChar);
                }
            }
        }
        return new String[]{""}; // 如果没有找到路径,返回空数组或相应的错误信息
    }
 
    static class Pair {
        char node;
        Character parent;
 
        public Pair(char node, Character parent) {
            this.node = node;
            this.parent = parent;
        }
    }
}
  1. JavaScript:



function findShortestPath(graph, A, B) {
    let visited = new Set();
    let queue = [];
    let parentMap = new Map();
 
    queue.push({ node: A, parent: null });
    visited.

评论已关闭

推荐阅读

Vue中使用mind-map实现在线思维导图
2024年08月04日
VUE
Web前端最全Vue实现免密登录跳转的方式_vue怎么样不登录返回首页,最强技术实现
2024年08月04日
VUE
vue3 项目搭建教程(基于create-vue,vite,Vite + Vue)
2024年08月04日
VUE
Vue-颜色选择器实现方案——>Vue-Color( 实战*1+ Demo*7)
2024年08月04日
VUE
Vue项目卡顿慢加载?这些优化技巧告诉你!_vue数据多渲染卡顿
2024年08月04日
VUE
vue中的keep-alive详解与应用场景
2024年08月04日
VUE
Vue、React实现excel导出功能(三种实现方式保姆级讲解)
2024年08月04日
vue-office/docx插件实现docx文件预览
2024年08月04日
VUE
java调用js文件的两种方法(支持V8引擎)
2024年08月04日
JavaScript:解决计算精度问题/mathjs/bignumber.js/big.js/decimal.js
2024年08月04日
两周从爬虫小白变大神 _yjs_js_security_passport
2024年08月04日
JS笔记(对象、函数、数组)
2024年08月04日
Markdown.js:强大的纯JavaScript Markdown解析器
2024年08月04日
Vue项目:js模拟点击a标签下载文件并重命名,URL文件地址下载方法、请求接口下载文件方法总结。
2024年08月04日
vue 父组件怎么获取子组件里面的data数据
2024年08月04日
VUE
个人开发实现AI套壳网站快速搭建(Vue+elementUI+SpringBoot)
2024年08月04日
el-table 表格封装并改造实现单元格可编辑
2024年08月04日
none
nodejs环境下创建vue项目、SSH密钥登陆!!!
2024年08月04日
vue+quill+element-ui实现视频、图片上传及缩放保姆级教程,轻松使用富文本
2024年08月04日
【three.js】22. Imported Models导入模型
2024年08月04日