• Source
    1. q.push(strt);
    2. bfs(strt);
    3.  
    4.  
    5. void bfs(int node)
    6. {
    7. ///This segment works to return if goal acheived. (Main modification of Standerd algo).
    8. vis[node]=1;
    9. if(get_goal) return;
    10. if(node==goal)
    11. {
    12. path.push_back(node);
    13. get_goal=true;
    14. return;
    15. }
    16.  
    17. path.push_back(node);
    18. if(q.empty()) return;
    19. int x=q.front();
    20. q.pop();
    21. for(int i=1;i<=n;i++)
    22. {
    23. if(graph[x][i] && !vis[i])
    24. {
    25. q.push(i);
    26. }
    27. }
    28. bfs(q.front());
    29. }