[CareerCup] 4.2 Route between Two Nodes in Directed Graph 有向图中两点的路径
4.2 Given a directed graph, design an algorithm to find out whether there is a route between two nodes.
LeetCode和CareerCup中关于图的题都不是很多,LeetCode中只有三道,分别是Clone Graph 无向图的复制,Course Schedule 课程清单 和 Course Schedule II 课程清单之二。目前看来CareerCup中有关图的题在第四章中仅此一道,这是一道关于有向图的题,书中是用java来做的,我们用c++来做时要定义图和节点,这里参考了之前那道Clone Graph 无向图的复制中关于无向图的定义,并且在里面加入了一个枚举类变量state,来帮助我们遍历。这种找两点之间路径的题就是遍历的问题,可以用BFS或DFS来解,先来看BFS的解法,如下所示:
//Definition for directed graph.
enum State {
Unvisited, Visited, Visiting
}; struct DirectedGraphNode {
int label;
State state;
vector<DirectedGraphNode *> neighbors;
DirectedGraphNode(int x) : label(x) {};
}; struct DirectedGraph {
vector<DirectedGraphNode*> nodes;
}; class Solution {
public:
bool search(DirectedGraph *g, DirectedGraphNode *start, DirectedGraphNode *end) {
queue<DirectedGraphNode*> q;
for (auto a : g->nodes) a->state = Unvisited;
start->state = Visiting;
q.push(start);
while (!q.empty()) {
DirectedGraphNode *node = q.front(); q.pop();
for (auto a : node->neighbors) {
if (a->state == Unvisited) {
if (a == end) return true;
else {
a->state = Visiting;
q.push(a);
}
}
}
node->state = Visited;
}
return false;
}
};
[CareerCup] 4.2 Route between Two Nodes in Directed Graph 有向图中两点的路径的更多相关文章
- Lintcode: Route Between Two Nodes in Graph
Given a directed graph, design an algorithm to find out whether there is a route between two nodes. ...
- Route Between Two Nodes in Graph
Given a directed graph, design an algorithm to find out whether there is a route between two nodes. ...
- [Swift]LeetCode882. 细分图中的可到达结点 | Reachable Nodes In Subdivided Graph
Starting with an undirected graph (the "original graph") with nodes from 0 to N-1, subdivi ...
- LeetCode 024 Swap Nodes in Pairs 交换链表中相邻的两个节点
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2-> ...
- [LeetCode] 882. Reachable Nodes In Subdivided Graph 细分图中的可到达结点
Starting with an undirected graph (the "original graph") with nodes from 0 to N-1, subdivi ...
- 882. Reachable Nodes In Subdivided Graph
题目链接 https://leetcode.com/contest/weekly-contest-96/problems/reachable-nodes-in-subdivided-graph/ 解题 ...
- 关于app.js和route.js和service.js还有controller.js中的依赖关系
2.只要是由路由去执行的的控制器模块,必须注入到app.js里面进行依赖,在页面上就不需要ng-controller在html页面上写了: 但是如果一个控制器模块,没有经过路由管理:那么就必须要, ...
- CareerCup All in One 题目汇总 (未完待续...)
Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...
- CareerCup All in One 题目汇总
Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...
随机推荐
- Swift中的字典
学习来自<极客学院:Swift中的字典> 工具:Xcode6.4 直接上基础的示例代码,多敲多体会就会有收获:百看不如一敲,一敲就会 import Foundation //字典的声明 v ...
- winform dateTimePicker选择时间控件-选择小时、分钟、秒
今天对公司项目进行改版(一个c/s客户端程序),要求dateTimePicker 能够选择小时,分钟.但找了很久,发现没有相关的简化控件,都是web的,没有winform的. 可是功夫不负有心人啊. ...
- ubuntu15.10 给解压版的eclipse安装桌面快捷方式
在桌面用vi 建立eclipse.desktop文件,并赋予权限 sudo chmod u+x /home/liujl/Desktop/eclipse.desktop [Desktop Entry ...
- 强制IE使用最高版本引擎渲染页面,避免默认使用IE7引擎导致的页面布局混乱及其它问题
背景 基于Asp.net MVC的一个Intranet web application, 现象 Application发布到服务器端后,在客户端IE访问页面布局混乱,并有javascript报错 原因 ...
- Effective Java 48 Avoid float and double if exact answers are required
Reason The float and double types are particularly ill-suited for monetary calculations because it i ...
- JS生成UUID的方法实例
<!DOCTYPE html> <html> <head> <script src="http://libs.baidu.com/jquery/1. ...
- linux: 获取监听指定端口的进程PID
在 linux 下经常需要杀死(重启)监听某端口的进程, 因此就写了一个小脚本, 通过 ss 命令获取监听制定端口的进程 PID, 然后通过 kill 命令结束掉进程: #!/bin/sh # set ...
- VirtualBox: Effective UID is not root (euid=1000 egid=100 uid=1000 gid=100)
桌面上运行virtualbox出错: The virtual machine 'xp' has terminated unexpectedly during startup with exit cod ...
- js清除缓存方法
1.加入如下头描述 <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <META HTTP-EQUI ...
- 【开学季】自学嵌入式开发|四核开发板|4412开发板|ARM+Android+linux技术
淘宝店铺:迅为开发板http://arm-board.taobao.com 网站:http://www.topeetboard.com QQ咨询:2551456065 电话咨询:010-5895758 ...