[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 ...
随机推荐
- 谷歌浏览器 模拟微信浏览器user-agent
1.F12 2.Elments->Emulation Media: Other Network:Mozilla/5.0 (Linux; Android 4.4.4; HM NOTE 1LT ...
- nginx配置入门
谢谢作者的分享精神,原文地址:http://www.nginx.cn/591.html nginx配置入门 之前的nginx配置是对nginx配置文件的具体含义进行讲解,不过对于nginx的新手可能一 ...
- docker入门(1) Centos 7 下docker的安装
centos 7安装docker 什么是 Docker Docker 是一个开源项目,诞生于 2013 年初,最初是 dotCloud 公司内部的一个业余项目.它基于 Google 公司推出的 Go ...
- XtraScheduler 日程控件显示自定义标题
下面代码实现一个自定义日程标题 public class CustomHeaderCaptionService : HeaderCaptionServiceWrapper { public Custo ...
- 【VB超简单入门】二、知识准备
在开始编程之前,需要先熟悉一下各种操作和术语,以后学习编程才能得心应手. 首先最重要的操作当然就是-电脑的开机关机啦~(开个玩笑哈哈),必须掌握软件的安装和卸载,还有能编写批处理程序对平时的使用也是很 ...
- matchesSelector及低版本IE中对该方法的实现
matchesSelector用来匹配dom元素是否匹配某css selector.它为一些高级方法的实现提供了基础支持,比如事件代理,parent, closest等. W3C在2006年就提出了该 ...
- 面试之jsp、Servlet相关知识——生命周期, 区别等
1.servlet生命周期 所谓生命周期,指的是servlet容器如何创建servlet实例.分配其资源.调用其方法.并销毁其实例的整个过程. 阶段一: 实例化(就是创建servlet对象,调用构造器 ...
- seq 显示00 01的格式
for i in `seq -w 00 20` ; do echo $i ;done 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 ...
- c++字符串互相转换
1.string vs char* //string to char* string str; const char* cch = str.c_str(); ]; strcpy(ch,cch); // ...
- 用c/c++混合编程方式为ios/android实现一个自绘日期选择控件(一)
本文为原创,如有转载,请注明出处:http://www.cnblogs.com/jackybu 前言 章节: 1.需求描述以及c/c++实现日期和月历的基本操作 2.ios实现自绘日期选择控件 3.a ...