深度优先搜索

深度优先搜索

深度优先搜索

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
#include "graph.hpp"
#include <stack>

void dfs(Node *node)
{
if (node == nullptr)
{
return;
}
stack<Node *> sta;
unordered_set<Node *> hashset;
sta.push(node);
hashset.insert(node);
// todo
cout << node->value << endl;
while (!sta.empty())
{
Node *cur = sta.top();
sta.pop();
for (auto &next : cur->nexts)
{
if (hashset.find(next) == hashset.end())
{
sta.push(cur);
sta.push(next);
hashset.insert(next);
// todo
cout << next->value << endl;
break;
}
}
}
}

int main()
{
}

深度优先搜索
http://example.com/2023/08/29/深度优先搜索/
作者
WHC
发布于
2023年8月29日
许可协议