Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
missionaries and cannibals
Author Message
Tubs

Posted: 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.

Tubs

Posted: 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: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 2 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: