-
Notifications
You must be signed in to change notification settings - Fork 0
/
2405.cpp
49 lines (46 loc) · 974 Bytes
/
2405.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <cstdio>
#include <cstdlib>
bool search (Graph g, Node start, Node end) {
Queue q = new Queue;
for each Node in Graph g
state[n] = NOT_VISITED;
state[start] = VISITING;
q.enqueue(start);
while(!q.empty()) {
n = q.dequeue;
for each v in n.adjacent_vertices{
if state[v] = NOT_VISITED {
if (v == end)
return true;
else {
state[v] = VISITING;
q.enqueue(v);
}
}
}
state[n] = VISITED;
}
return false;
}
bool search(Graph g, Node start, Node end) {
Stack q = new Stack;
for each Node in Graph
state [n] = NOT_VISITED;
q.push (start);
state[start] = VISITING;
while(!q.empty) {
n = q.pop();
for each v in n.adjacent_vertices{
if state[v] == NOT_VISITED {
if (v==end)
return true;
else {
state[v] = VISITING;
q.push(v) ;
}
}
}
}
state[n] = VISITED;
}