Advertisement
symonhasan

Depth Limit Search

Jan 9th, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. // DEPTH LIMIT SEARCH
  2. /*
  3. 15 14
  4. 1 2
  5. 1 3
  6. 2 4
  7. 2 5
  8. 3 6
  9. 3 7
  10. 4 8
  11. 4 9
  12. 5 10
  13. 5 11
  14. 6 12
  15. 6 13
  16. 7 14
  17. 7 15
  18. 1
  19. 12
  20. 3
  21. */
  22. #include<bits/stdc++.h>
  23. using namespace std;
  24. int level , goal;
  25. vector < int > G[ 10005 ];
  26. bool visited[ 10005 ];
  27.  
  28. void _DFS( int src , int l )
  29. {
  30.     if( visited[ goal ] == 1 )
  31.         return;
  32.     cout << src << " -> ";
  33.  
  34.     visited[ src ] = 1;
  35.  
  36.  
  37.     for( int i = 0; i < G[ src ].size(); i++ )
  38.     {
  39.         if( !visited[ G[ src ][ i ] ] && l + 1 <= level )
  40.         {
  41.             _DFS( G[ src ][ i ] , l + 1 );
  42.         }
  43.     }
  44. }
  45.  
  46. int main()
  47. {
  48.     freopen("input.txt" , "r" , stdin );
  49.     int node , edge;
  50.     cin >> node >> edge;
  51.  
  52.     for( int i = 0; i < edge; i++ )
  53.     {
  54.         int u , v;
  55.         cin >> u >> v;
  56.         G[ u ].push_back( v );
  57.         G[ v ].push_back( u );
  58.     }
  59.     int src;
  60.     cin >> src;
  61.     cin >> goal >> level;
  62.     _DFS( src , 0 );
  63.     cout << "\n";
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement