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