Advertisement
logicmoo

MandC

Jul 1st, 2018
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 2.77 KB | None | 0 0
  1. % to run the code in SWI-Prolog, do
  2. %        ?- ['missionaries_and_cannibals.pl'].
  3. %        ?- find.
  4.  
  5. % Represent a state as world(CL,ML,B,CR,MR)
  6.  
  7. start(world(3,3,left,0,0)).
  8.  
  9. goal(world(0,0,right,3,3)).
  10.  
  11. legal(CL,ML,CR,MR) :-
  12.     % is this state a legal one?
  13.     ML>=0, CL>=0, MR>=0, CR>=0,
  14.     (ML>=CL ; ML=0),
  15.     (MR>=CR ; MR=0).
  16.  
  17. % Possible moves:
  18. move(world(CL,ML,left,CR,MR),world(CL,ML2,right,CR,MR2)):-
  19.     % Two missionaries cross left to right.
  20.     MR2 is MR+2,
  21.     ML2 is ML-2,
  22.     legal(CL,ML2,CR,MR2).
  23.  
  24. move(world(CL,ML,left,CR,MR),world(CL2,ML,right,CR2,MR)):-
  25.     % Two cannibals cross left to right.
  26.     CR2 is CR+2,
  27.     CL2 is CL-2,
  28.     legal(CL2,ML,CR2,MR).
  29.  
  30. move(world(CL,ML,left,CR,MR),world(CL2,ML2,right,CR2,MR2)):-
  31.     %  One missionary and one cannibal cross left to right.
  32.     CR2 is CR+1,
  33.     CL2 is CL-1,
  34.     MR2 is MR+1,
  35.     ML2 is ML-1,
  36.     legal(CL2,ML2,CR2,MR2).
  37.  
  38. move(world(CL,ML,left,CR,MR),world(CL,ML2,right,CR,MR2)):-
  39.     % One missionary crosses left to right.
  40.     MR2 is MR+1,
  41.     ML2 is ML-1,
  42.     legal(CL,ML2,CR,MR2).
  43.  
  44. move(world(CL,ML,left,CR,MR),world(CL2,ML,right,CR2,MR)):-
  45.     % One cannibal crosses left to right.
  46.     CR2 is CR+1,
  47.     CL2 is CL-1,
  48.     legal(CL2,ML,CR2,MR).
  49.  
  50. move(world(CL,ML,right,CR,MR),world(CL,ML2,left,CR,MR2)):-
  51.     % Two missionaries cross right to left.
  52.     MR2 is MR-2,
  53.     ML2 is ML+2,
  54.     legal(CL,ML2,CR,MR2).
  55.  
  56. move(world(CL,ML,right,CR,MR),world(CL2,ML,left,CR2,MR)):-
  57.     % Two cannibals cross right to left.
  58.     CR2 is CR-2,
  59.     CL2 is CL+2,
  60.     legal(CL2,ML,CR2,MR).
  61.  
  62. move(world(CL,ML,right,CR,MR),world(CL2,ML2,left,CR2,MR2)):-
  63.     %  One missionary and one cannibal cross right to left.
  64.     CR2 is CR-1,
  65.     CL2 is CL+1,
  66.     MR2 is MR-1,
  67.     ML2 is ML+1,
  68.     legal(CL2,ML2,CR2,MR2).
  69.  
  70. move(world(CL,ML,right,CR,MR),world(CL,ML2,left,CR,MR2)):-
  71.     % One missionary crosses right to left.
  72.     MR2 is MR-1,
  73.     ML2 is ML+1,
  74.     legal(CL,ML2,CR,MR2).
  75.  
  76. move(world(CL,ML,right,CR,MR),world(CL2,ML,left,CR2,MR)):-
  77.     % One cannibal crosses right to left.
  78.     CR2 is CR-1,
  79.     CL2 is CL+1,
  80.     legal(CL2,ML,CR2,MR).
  81.  
  82.  
  83. % Recursive call to solve the problem
  84. path(world(CL1,ML1,B1,CR1,MR1),world(CL2,ML2,B2,CR2,MR2),Explored,MovesList) :-
  85.    move(world(CL1,ML1,B1,CR1,MR1),world(CL3,ML3,B3,CR3,MR3)),
  86.    not(member(world(CL3,ML3,B3,CR3,MR3),Explored)),
  87.    path(world(CL3,ML3,B3,CR3,MR3),world(CL2,ML2,B2,CR2,MR2),[world(CL3,ML3,B3,CR3,MR3)|Explored],
  88.     [ [ world(CL3,ML3,B3,CR3,MR3), world(CL1,ML1,B1,CR1,MR1)] | MovesList ]).
  89.  
  90. % Solution found
  91. path(world(CL,ML,B,CR,MR),world(CL,ML,B,CR,MR),_,MovesList):-
  92.     output(MovesList).
  93.  
  94. % Printing
  95. output([]) :- nl.
  96. output([[A,B]|MovesList]) :-
  97.     output(MovesList),
  98.     write(B), write(' -> '), write(A), nl.
  99.  
  100. % Find the solution for the missionaries and cannibals problem
  101. find :-
  102.    path(world(3,3,left,0,0),world(0,0,right,3,3),[world(3,3,left,0,0)],_).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement