Advertisement
sheyshya1

water jug problem

Dec 4th, 2021
1,319
-1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 2.08 KB | None | 0 1
  1. database
  2. visited_state(integer,integer)
  3. predicates
  4. nondeterm state(integer,integer)
  5. clauses
  6. state(2,0).
  7. state(X,Y):- X < 4,
  8. not(visited_state(4,Y)),
  9. assert(visited_state(X,Y)),
  10. write("Fill the 4-Gallon Jug: (",X,",",Y,") --> (", 4,",",Y,")\n"),
  11. state(4,Y).
  12. state(X,Y):- Y < 3,
  13. not(visited_state(X,3)),
  14. assert(visited_state(X,Y)),
  15. write("Fill the 3-Gallon Jug: (", X,",",Y,") --> (", X,",",3,")\n"),
  16. state(X,3).
  17. state(X,Y):- X > 0,
  18. not(visited_state(0,Y)),
  19. assert(visited_state(X,Y)),
  20. write("Empty the 4-Gallon jug on ground: (", X,",",Y,") --> (", 0,",",Y,")\n"),
  21. state(0,Y).
  22. state(X,Y):- Y > 0,
  23. not(visited_state(X,0)),
  24. assert(visited_state(X,0)),
  25. write("Empty the 3-Gallon jug on ground: (", X,",",Y,") --> (", X,",",0,")\n"),
  26. state(X,0).
  27.  
  28. state(X,Y):- X + Y >= 4,
  29. Y > 0,
  30. NEW_Y = Y - (4 - X),
  31. not(visited_state(4,NEW_Y)),
  32. assert(visited_state(X,Y)),
  33. write("Pour water from 3-Gallon jug to 4-gallon until it is full: (", X,",",Y,") --> (",
  34. 4,",",NEW_Y,")\n"),
  35. state(4,NEW_Y).
  36. state(X,Y):- X + Y >=3,
  37. X > 0,
  38. NEW_X = X - (3 - Y),
  39. not(visited_state(X,3)),
  40. assert(visited_state(X,Y)),
  41. write("Pour water from 4-Gallon jug to 3-gallon until it is full: (", X,",",Y,") --> (",
  42. NEW_X,",",3,")\n"),
  43. state(NEW_X,3).
  44. state(X,Y):- X + Y <=4,
  45. Y > 0,
  46. NEW_X = X + Y,
  47. not(visited_state(NEW_X,0)),
  48. assert(visited_state(X,Y)),
  49. write("Pour all the water from 3-Gallon jug to 4-gallon: (", X,",",Y,") --> (", NEW_X,",",0,")\n"),
  50. state(NEW_X,0).
  51. state(X,Y):- X+Y<=3,
  52. X > 0,
  53. NEW_Y = X + Y,
  54. not(visited_state(0,NEW_Y)),
  55. assert(visited_state(X,Y)),
  56. write("Pour all the water from 4-Gallon jug to 3-gallon: (", X,",",Y,") --> (", 0,",",NEW_Y,")\n"),
  57.  
  58. state(0,NEW_Y).
  59. state(0,2):- not(visited_state(2,0)),
  60. assert(visited_state(0,2)),
  61. write("Pour 2 gallons from 3-Gallon jug to 4-gallon: (", 0,",",2,") --> (", 2,",",0,")\n"),
  62. state(2,0).
  63. state(2,Y):- not(visited_state(0,Y)),
  64. assert(visited_state(2,Y)),
  65. write("Empty 2 gallons from 4-Gallon jug on the ground: (", 2,",",Y,") --> (", 0,",",Y,")\n"),
  66. state(0,Y).
  67. goal
  68. write("Initially state(0,0)"),nl,
  69. state(0,0),
  70. save("E:\output.txt").
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement