// dfs.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <stack>
#include <map>
#include <vector>
using namespace std;
#define NotFound -1
#define StartNode 0
#define EndNode 4
map<int,map<int,int>> visited;
map<int,vector<int>> ajdTable;
int isInStack[5];
//不在栈中且路径没有访问过
int getNextPoint(int point){
vector<int>::iterator it;
for ( it=ajdTable[point].begin() ; it != ajdTable[point].end(); it++ ){
if ((visited[point][*it]!=1) && (*it == EndNode))
return *it;
if ((visited[point][*it]!=1) && (isInStack[*it]!=1))
return *it;
}
return NotFound;
}
void setNoVisted(int point){
vector<int>::iterator it;
for ( it=ajdTable[point].begin() ; it != ajdTable[point].end(); it++ ){
visited[point][*it] = 0;
}
}
void dfs_stack(int start, int end, int n){
int s_top,n_point;
for(int i = 0;i < n;
非递归求两点之间的路径(无向图)
最新推荐文章于 2024-04-19 14:57:16 发布
此代码实现了一个深度优先搜索(DFS)算法,用于在一个无向图中找出从起点0到终点4的所有路径。通过维护一个栈来跟踪节点,并使用visited映射记录已访问的节点状态,确保不重复访问。当找到目标节点时,它将输出路径。

最低0.47元/天 解锁文章
759

被折叠的 条评论
为什么被折叠?



