lintcode:Route Between Two Nodes in Graph
来源:互联网 发布:淘宝上买流量怎么退款 编辑:程序博客网 时间:2024/05/27 00:48
Given a directed graph, design an algorithm to find out whether there is a route between two nodes.
Example
Given graph:
A----->B----->C \ | \ | \ | \ v ->D----->E
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 if (s->neighbors.size() == 0) return false; if (s == t) return true; deque<DirectedGraphNode *> bfs; bfs.push_back(s); while (bfs.size() > 0) { DirectedGraphNode* curNode = bfs[0]; for (int i=0; i<curNode->neighbors.size(); i++) { if (curNode->neighbors[i] == t) { return true; } else { bfs.push_back(curNode->neighbors[i]); } } bfs.pop_front(); } return false; }};
0 0
- lintcode:Route Between Two Nodes in Graph
- DFS-----Route Between Two Nodes in Graph
- #176 Route Between Two Nodes in Graph
- Route Between Two Nodes in Graph
- Route Between Two Nodes in Graph
- Lintcode176 Route Between Two Nodes in Graph solution 题解
- 176.Route Between Two Nodes in Graph-图中两个点之间的路线(中等题)
- 4.1 Route Between Nodes
- LintCode:Swap Two Nodes in Linked List
- [LintCode]Swap Two Nodes in Linked List
- Finding Path/Route Between two points on MapKit in iPhone
- find distance between two nodes in a binary tree
- Lowest Common Ancestor (LCA) of two nodes in graph
- How To Get Min-Cost Between two points in graph (Dijkstra’s algorithm)
- Finding all possible simple paths (paths without cycles) between two vertices in a graph
- Find the distance between two nodes
- [Lintcode]Swap Nodes in Pairs
- [LintCode] Swap Two Nodes in Linked List 交换链表中的两个结点
- 相似图片搜索原理三(颜色直方图—c++实现)
- 任何值得去的地方,都没有捷径。
- 一个IP能建立的最大连接数是多少?
- javabean在jsp中的使用
- 队列的链式结构C语言实现
- lintcode:Route Between Two Nodes in Graph
- JavaScript:查找/匹配
- wireshark简单使用
- 未找到System.Runtime.InteropServices.Marshal.GetTypeFromCLSID(System.Guid) 方法错误
- DWR+SpringMVC整合的3种方式之一
- 自定义结构--二叉排序树
- 学习交流
- DuiVision开发教程(8)-控件的快捷键和焦点的支持
- MongoDB简介