0
をされ、私は私が何を行うことができますBFSどのようなレベルの検索でBFS(幅優先検索)を使用しているのですか?だから、これは使用したアルゴリズムイム
void bfs(int n)
{
vis[n]=1; //marks n visited
d=0;
while(!adj[n].empty()) //adj is the array containing the adjacency lists
{if(!(vis[adj[n].front()]))
{
q.push(adj[n].front()); //q is the queue
}
adj[n].pop_front();
}
if(!q.empty()){
n=q.front();
cout<<n<< "->";
q.pop();
bfs(n);
}
}
を使用しています深さのどのレベルに知りたいですか?
ちょうど余分 'depth'パラメータに沿って通過を置く必要があります。 'bfs'の最初の呼び出しで0を渡します。再帰呼び出しでは、' depth + 1'を渡します。したがって、 'void bfs(int n、int depth){... bfs(n、depth + 1); ...} ' –