forked from codesession/Data-structure-Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphDFS.cpp
More file actions
55 lines (49 loc) · 946 Bytes
/
GraphDFS.cpp
File metadata and controls
55 lines (49 loc) · 946 Bytes
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
50
51
52
53
54
55
#include<iostream>
#include<stack>
using namespace std;
const static int INF = __INT_MAX__;
/*
A B
/ \ /
/ \ /
/ C------D
E /
\ /
\ /
F
*/
int matrix[6][6] = {{INF, INF, 1, INF, 1, INF},
{INF, INF, 1, INF, INF, INF},
{1, 1, INF, 1, INF, INF},
{INF, INF, 1, INF, INF, 1},
{1, INF, INF, INF, INF, 1},
{INF, INF, INF, 1, 1, INF}
};
int visited[6] = {0};
void DFS(stack<char>& s, char ch)
{
if (s.empty())
return;
cout << ch;
for (int i = 0; i < 6; i++)
{
if (matrix[ch - 'A'][i] == 1 && visited[i] == 0)
{
visited[i] = 1;
s.push('A' + i);
DFS(s, s.top());
}
}
s.pop();
}
int main(int argc, char const *argv[])
{
stack<char> s;
char begin = 'A';
cout << "DFS begin from A:\n";
s.push(begin);
visited[0] = 1;
DFS(s, s.top());
cout << endl;
return 0;
}