图中两个点之间的路线 -LintCode
来源:互联网 发布:打豆豆小游戏优化版 编辑:程序博客网 时间:2024/05/18 02:28
给出一张有向图,设计一个算法判断两个点 s 与 t 之间是否存在路线。
样例
如下图:
for s = B and t = E, return true
for s = D and t = C, return false
/** * Definition for Directed graph. * struct DirectedGraphNode { * int label; * vector<DirectedGraphNode *> neighbors; * DirectedGraphNode(int x) : label(x) {}; * }; */class Solution {public: /** * @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 */ bool hasRoute(vector<DirectedGraphNode*> graph, DirectedGraphNode* s, DirectedGraphNode* t) { // write your code here queue<DirectedGraphNode*> que; map<DirectedGraphNode*, bool> visited; que.push(s); visited[s]=true; while (!que.empty()) { DirectedGraphNode* node = que.front(); que.pop(); if (node == t) return true; for (auto c : node->neighbors) { if (!visited[c]) { que.push(c); visited[c] = true; } } } 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计算两个点之间夹角计算
- 15. ar
- 拉格朗日乘子法与KKT条件
- Havel-Hakimi定理
- SASS界面编译工具——Koala的使用
- 字符串数组
- 图中两个点之间的路线 -LintCode
- 注解Annotation
- ViewPager以及ViewFlipper的简单应用
- 利用mybatis生成pojo,dao层,xml文件的相关配置
- 非极大值抑制原理
- POJ2774(二分+哈希)
- 从小白开始入门python+tensorflow+cnn做人脸性别识别(一)
- hdu2774(后缀数组 最长公共子串)Long Long Message
- 关于递归算法的几个例子(C语言)