Guest User

Untitled

a guest
Jan 20th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. bool DFS (currentState) =
  2. {
  3. if (myHashSet.Contains(currentState))
  4. {
  5. return;
  6. }
  7. else
  8. {
  9. myHashSet.Add(currentState);
  10. }
  11. if (IsSolution(currentState) return true;
  12. else
  13. {
  14. for (var nextState in GetNextStates(currentState))
  15. if (DFS(nextState)) return true;
  16. }
  17. return false;
  18. }
  19.  
  20. bool DFS (currentState, maxDepth) =
  21. {
  22. if (maxDepth = 0) return false;
  23. if (myHashSet.Contains(currentState))
  24. {
  25. return;
  26. }
  27. else
  28. {
  29. myHashSet.Add(currentState);
  30. }
  31. if (IsSolution(currentState) return true;
  32. else
  33. {
  34. for (var nextState in GetNextStates(currentState))
  35. if (DFS(nextState, maxDepth - 1)) return true;
  36. }
  37. return false;
  38. }
  39.  
  40. A - B - C - D - E - A
  41. |
  42. F - G (Goal)
  43.  
  44. bool DFS (currentState, maxDepth) =
  45. {
  46. if (maxDepth = 0) return false;
  47. if (myHashSet.Contains(currentState))
  48. {
  49. return;
  50. }
  51. else
  52. {
  53. myHashSet.Add(currentState);
  54. }
  55. if (IsSolution(currentState) return true;
  56. else
  57. {
  58. for (var nextState in GetNextStates(currentState))
  59. if (DFS(nextState, maxDepth - 1)) return true;
  60. }
  61. myHashSet.Remove(currentState); //the state is pop out from the stack
  62. return false;
  63. }
Add Comment
Please, Sign In to add comment