题目:图中两个点之间的路线
来源:互联网 发布:linux get_sb 编辑:程序博客网 时间:2024/05/17 23:37
给出一张有向图,设计一个算法判断两个点 s 与 t 之间是否存在路线。
您在真实的面试中是否遇到过这个题?
Yes
样例
如下图:
A----->B----->C
\ |
\ |
\ |
\ v
->D----->E
for s = B and t = E, return true
for s = D and t = C, return false
标签 Expand
相关题目 Expand
您在真实的面试中是否遇到过这个题?
Yes
样例
如下图:
A----->B----->C
\ |
\ |
\ |
\ v
->D----->E
for s = B and t = E, return true
for s = D and t = C, return false
标签 Expand
相关题目 Expand
解题思路:
宽度优先搜索
/*** Definition for Directed graph.* class DirectedGraphNode {* int label;* ArrayList<DirectedGraphNode> neighbors;* DirectedGraphNode(int x) {* label = x;* neighbors = new ArrayList<DirectedGraphNode>();* }* };*/public class Solution { /** * @param graph: A list of Directed graph node * @param s: the starting Directed graph node * @param t: the terminal Directed graph node * @return: a boolean value */ public boolean hasRoute(ArrayList<DirectedGraphNode> graph, DirectedGraphNode s, DirectedGraphNode t) { // write your code here if(0==graph.size()||null==graph) return false; Queue<DirectedGraphNode> curNodes = new LinkedList<DirectedGraphNode>(); curNodes.add(s); while(null!=curNodes&&curNodes.size()>0){ DirectedGraphNode curNode = curNodes.poll(); if(curNode!=null){ if(curNode==t){ return true; }else { ArrayList<DirectedGraphNode> neighbors = curNode.neighbors; for(DirectedGraphNode node:neighbors){ curNodes.add(node); } } } } return false; }}
0 0
- 题目:图中两个点之间的路线
- 图中两个点之间的路线
- 图中两个点之间的路线
- LintCode-图中两个点之间的路线
- lintcode-图中两个点之间的路线-176
- LintCode:图中两个点之间的路线
- lintcode -- 图中两个点之间的路线
- 图中两个点之间的路线 -LintCode
- lintcode 图中两个点之间的路线 广度优先搜索
- 176.Route Between Two Nodes in Graph-图中两个点之间的路线(中等题)
- 计算蜂窝中两个点之间的距离
- 计算两个坐标点之间的距离
- 计算两个坐标点之间的距离
- ACM2001Java实现两个点之间的距离
- 在百度地图API开发中已知两个点之间的经纬度坐标计算其距离
- 在百度地图API开发中已知两个点之间的经纬度坐标计算其距离
- Altium Designer中测量Gerber文件两个点之间的距离
- cocos2dx中lua计算两个点之间夹角计算
- 贝塞尔曲线
- 构造数独
- JavaWeb——JavaScript_总结
- Linux 的目录结构
- 【转】python经典面试题目
- 题目:图中两个点之间的路线
- 记在学习swift语言进行iOS开发前
- XML vs JSON 浅析
- python基础学习笔记
- java如何获得唯一表示字符串
- Spring @Transactional
- 第二章 :Hello World 应用程序详解
- nginx的请求处理机制
- JavaWeb——JavaScript常用正则表达式