176. Route between two points in the picture
Given a directed graph, design an algorithm to determine whether there is a route between two points s and t.
Sample
As shown below:
A----->B----->C
\ |
\ |
\ |
\ v
->D----->E
Example 1:
Input: s = B and t = E,
output: true
Example 2:
Input: s = D and t = C,
output: 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
std::queue<DirectedGraphNode *> dQueue;
dQueue.push(s);
while(!dQueue.empty())
{
DirectedGraphNode * node = dQueue.front();
dQueue.pop();
if(node->label == t->label)
{
return true;
}
for(int i=0;i<node->neighbors.size();i++)
{
dQueue.push(node->neighbors[i]);
}
}
return false;
}
};