fork download
  1. #include<bits/stdc++.h>
  2. using namespace std ;
  3.  
  4. vector<char> v[5] = {
  5. {'b', 'c'},
  6. {'a', 'c', 'd'},
  7. {'a', 'b', 'd', 'e'},
  8. {'b', 'c', 'e'},
  9. {'c', 'd'},
  10. };
  11.  
  12. char starting = 'a', ending = 'e' ;
  13. bool visited [5] ;
  14. vector<char> ans ;
  15.  
  16. bool dfs (char c) {
  17. visited[c - 'a'] = true ;
  18. ans.push_back(c) ;
  19.  
  20. if (c == ending) {
  21. return true ;
  22. }
  23.  
  24. for (auto i : v[c - 'a']) {
  25. if (!visited[i - 'a']) {
  26. if (dfs(i)) {
  27. return true ;
  28. }
  29. }
  30. }
  31.  
  32. ans.pop_back() ;
  33. return false ;
  34. }
  35.  
  36. int main () {
  37. if (dfs(starting)) {
  38. for (auto &i : ans) {
  39. cout << i << " " ;
  40. }
  41. }
  42. else {
  43. cout << "Can't reach the Ending point" ;
  44. }
  45. return 0 ;
  46. }
  47.  
  48. /*
  49.  * answer is :[ a b c d e ]
  50.  */
Success #stdin #stdout 0.01s 5212KB
stdin
Standard input is empty
stdout
a b c d e