fork download
  1. #include <vector>
  2. #include <list>
  3. #include <map>
  4. #include <set>
  5. #include <deque>
  6. #include <stack>
  7. #include <bitset>
  8. #include <algorithm>
  9. #include <functional>
  10. #include <numeric>
  11. #include <utility>
  12. #include <sstream>
  13. #include <iostream>
  14. #include <iomanip>
  15. #include <cstdio>
  16. #include <cmath>
  17. #include <cstdlib>
  18. #include <ctime>
  19. #include <unordered_set>
  20. #define debug(a) { cout<< #a << " " << a << " " ;}
  21. using namespace std;
  22.  
  23. bool onedist(const string &a, const string &b);
  24. int dfs(string &begin, string &end, unordered_set<string>& words, bool visited[],int &counting);
  25. int ladderLength(string begin, string end, unordered_set<string>& words);
  26.  
  27. // Function that returns true if the two strings are at an edit distance of 1 from each other
  28. bool onedist(const string &a, const string &b) {
  29. int mismatch = 0;
  30. for(int i = 0;i< a.size(); i++) {
  31. if(a[i] == b[i])
  32. {
  33. // do nothing
  34. }
  35. else mismatch++;
  36.  
  37. if (mismatch > 1)
  38. return false;
  39. }
  40.  
  41. return true;
  42. }
  43.  
  44. int dfs(string &begin, string &end, vector<string>& wordlife, bool visited[],int counting) {
  45. //cout << "begin" << begin <<endl;
  46. if(onedist(begin,end)) {
  47.  
  48. return counting+1;
  49. // Should exit, but doesn't. Causing run time error
  50. }
  51.  
  52. int n = wordlife.size();
  53. int c=0,min=n+1;//min is used to find minimum possible transformations required
  54. for(int i = 0;i<n;i++)
  55. {
  56. if(!visited[i] && onedist(begin, wordlife[i])) {
  57. visited[i] = true;
  58. c=dfs(wordlife[i],end,wordlife,visited,counting+1);//we store the value returned by dfs in c so that we can find minimum required transformation
  59. //cout << "Test flow" << c << endl;
  60. if(c<min)
  61. min=c;//to find minimum transformations required
  62.  
  63. }
  64.  
  65. }
  66. if(c!=0)
  67. return min; //return min possible transformations
  68. else
  69. return 0;//if no possible transformations are possible
  70. }
  71.  
  72. int ladderLength(string begin, string end, vector<string>& wordlife)
  73. {
  74.  
  75. int n = wordlife.size();
  76. bool visited[n];
  77. for(int i = 0;i<n;i++)
  78. visited[i] = false;
  79. int count = 1;
  80.  
  81. int ans = dfs(begin,end,wordlife,visited,count);
  82.  
  83. return ans;
  84.  
  85.  
  86.  
  87. }
  88.  
  89. int main()
  90. {
  91. string begin = "hit";
  92. string end = "cog";
  93. vector<string> wordlife;
  94. wordlife.push_back("hot");
  95. wordlife.push_back("dot");
  96. wordlife.push_back("dog");
  97. wordlife.push_back("lot");
  98. wordlife.push_back("log");
  99. int a = ladderLength( begin, end, wordlife);
  100. cout <<a<<endl;
  101. return 0;//the run time error was because you were returning 'a' instead of 0
  102. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
5