Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 missionaries and cannibals
Index -> Programming, General Programming -> Logical Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tubs




PostPosted: Tue Feb 12, 2008 12:23 pm   Post subject: missionaries and cannibals

Quote:
In the missionaries and cannibals problem, three missionaries and three cannibals must cross a river using a boat which can carry at most two people, under the constraint that, for both banks, if there are missionaries present on the bank, they cannot be outnumbered by cannibals (if they were, the cannibals would eat the missionaries.) The boat cannot cross the river by itself with no people on board.


I have so far developed a program in Prolog to solve this problem, but the question requires that the program solve for all possible solutions. My first idea was to record the first solution and check that the next solution isn't identical to the first, but would this not just incur an infinate loop when the program restarts? I am a total noob at Prolog and the prof is sub-par, does anyone have any suggestions? I can provide code if necessary.
Sponsor
Sponsor
Sponsor
sponsor
Tubs




PostPosted: Wed Apr 09, 2008 10:25 am   Post subject: Re: missionaries and cannibals

Well, since this got no responses I can assume there is little working knowledge of prolog around. Hence: the solution!

code:
% Main control block and printing

find :-
   path([3,3,left],[0,0,right],[[3,3,left]],_).
output([]) :- nl, nl.
output([[A,B,String]|T]) :-
output(T),
   write(B), write(' ~~ '), write(A), write(': '), write(String), nl.


% Base case

path([A,B,C],[A,B,C],_,MoveList):-
nl,nl,output(MoveList).


% Recursive call to solve the problem

path([A,B,C],[D,E,F],Traversed,Moves) :-
   move([A,B,C],[I,J,K],Out),
   legal([I,J,K]),  % Don't use this move unless it's safe.
   not(member([I,J,K],Traversed)),
   path([I,J,K],[D,E,F],[[I,J,K]|Traversed],[ [[I,J,K],[A,B,C],Out] | Moves ]).

% Move commands and descriptions of the move

move([A,B,left],[C,B,right],'One missionary crosses the river') :-
   A > 0, C is A - 1.
move([A,B,left],[C,B,right],'Two missionaries cross the river') :-
   A > 1, C is A - 2.
move([A,B,left],[C,D,right],'One missionary and One cannibal cross the river') :-
   A > 0, B > 0, C is A - 1, D is B - 1.
move([A,B,left],[A,D,right],'One cannibal crosses the river') :-
   B > 0, D is B - 1.
move([A,B,left],[A,D,right],'Two cannibals cross the river') :-
   B > 1, D is B - 2.
move([A,B,right],[C,B,left],'One missionary returns from the other side') :-
   A < 3, C is A + 1.
move([A,B,right],[C,B,left],'Two missionaries return from the other side') :-
   A < 2, C is A + 2.
move([A,B,right],[C,D,left],'One missionary and One cannibal return from the other side') :-
   A < 3, B < 3, C is A + 1, D is B + 1.
move([A,B,right],[A,D,left],'One cannibal returns from the other side') :-
   B < 3, D is B + 1.
move([A,B,right],[A,D,left],'Two cannibals return from the other side') :-
   B < 2, D is B + 2.

% Legal move definition where B is missionaries and A is cannibals:

legal([B,A,_]) :-
   (A =< B ; B = 0),
   C is 3-A, D is 3-B,
   (C =< D; D = 0).
Display posts from previous:   
   Index -> Programming, General Programming -> Logical Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: