0
그래서이 알고리즘 메신저 사용하고, 나는 깊이있는 수준에 내가 BFS (Breadth-first search)를 사용하는 검색 수준을 어떻게 알 수 있습니까?
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);
}
}
내가 무엇을 할 수 BFS
사용하고 알고 싶어?
그냥 여분의'depth' 매개 변수를 전달할을 넣어해야합니다. 'bfs'에 대한 초기 호출에서 0을 전달하십시오. 재귀 호출에서'depth + 1'을 전달하십시오. 그러므로 : void bfs (int n, int depth) {... bfs (n, depth + 1); ...}' –