# Cannibals

and Missionaries

Ruby Lim Department of Mathematics The University of Meibourne Parkviiie, ViCTORiA 3052 AU STRALiA

Abstract

2. State

of

the

System

This paper describes the game of Cannibals and Missionaries modelled using APL68000, It constitutes the original version of a student project report for a fourth-year course titled “Operations Research mathematics
Modclling”.

This game is mathematically modelled with a state system that is represented by two variables, (c, m), where c = number of cannibals on the first bank of the river m = number river. of missionaries on the first bank of the

1. Introduction A group consisting of an equal number of cannibals and missionaries seeks to cross a river. A boat, which will hold a limited number of people, is available and can be navigated by any combination of cannibals and missionaries. .The object of this game is to transport the entire group across the river. If the cannibals outnumber the missionaries on either side of the river, or in the boat, then the outnumbered missionary or missionaries die(s). Obviously, we do not want this to happen. Say, a general case has N cannibals, N missionaries and M people in the boat, where N > M > 1. I will be examining different combinations of N and M. This mathematical model is often mistaken for a simpie game, where the solutions are simple and straight-forward. However, this is not the case. Complex steps are involved in the calculation of which states are feasible, which states have not been encountered before and how to generate the feasible solutions, if they exist at all. Using the tree diagrams, state diagrams and/or further mathematical analysis, solutions can be calculated by hand mathematically. Complications can arise unless the position of the boat is taken into consideration, ie which bank of the river it is on. Because of the many iterations, the computer is used to find the solutions, It is not necessary to give the number of cannibals and missionaries on the second bank, since the total number of each must always equal N. From this, transitions can be made from one state to another. The set of allowable states consists of all pairs (c, m) satisfying: 1. The number of people in the boat must be less than or equal to M. 2. The number of cannibals and missionwies banks and in the boat must satisfy: on both

c.c<morm=O. Therefore, the number of cannibals and missionaries on either bank and in the boat must satisfy:

b. OSm<N c.c=morm=Oorm=N. 3. Do not return to a state that has already been encountered (ie to prevent cycles or the problem may never Ix solved).

Permissionlo CODYwithout fee all or part of this material is granted provided that the mpies are not made or distributed for direct commerclaladvantage, the ACM copyright notica and ttw title of the publication copying IS by permission othefwise, or to republish, 01992 and Ite date appear, and notice is given that To copy of the Association for Computing Machinery, requires a fee andlor specific permission \$1.50

ACM 0-89791 -478-3 92/0007-0135

Ruby Lim

135

One must be careful to use only allowable states. Those shown by dotted lines should be ignored since they lead back to a state already encountered. 2 @oat is on: left bank (3. The state of the system can be illustrated geometrical representations. m). but the transition from (3. 2. lying in the shape of the letter Z. 1 (3. 1. 3) to (3. For the problem with three cannibals and three missionaries.3) (43) A Fig. 1 is constructed width-wise. m) satisfying a. 3) is possible by sending two cannibals across in the boat. Fig. let us first consider a specific case where there are 3 cannibals. 1) send two missionaries (5) river. This is called the s(ate (1. 1) is not allowable.3) (1 . 3) meaning across the river’ (2) (3) river (4) (1.3) return one cannibal across the c. but some of these are not allowable. State Diagrams (1) (3.3) This model can also be constructed using state diagrams. There are therefore 16 possible pairs (c. 3) to (1.()<m<N. diagram. which in turn produce the next generation of all possible states. using different m 3.To demonstrate the state transitions. and so on. 3 missionaries and a boat capacity of 2 at most. 2 or 3. 2 q q 1 &4-e---c 0123 Fig. 3) --> (O. 3) --> (1. 3 Cannibals and Missionaries 136 APL 92 . In Fig. because it would result in disaster for the one missions-y remaining on the first bank. There are ten states. The states are represented on a rectangular coordinate system.and (2.3) Evaluation by tree diagrams can be done width-wise or depth-wise. c and m can be O.2) i Iefi bank (3.c=morm=Oorm=N. 1. 3) --> (1. m) is plotted.3) m right bank (2. 3) --> (2. This set of states is shown in Fig. c 0123 Fig. 3) send two cannibals (O.3) (2. where a parent state produces atl the possible children states. 3) --> (1. The transition from (3. the first part of a tree diagram shows all possible transitions. Tree Diagrams A tree diagram constructs this mathematical model by branching from one state to produce the next state(s). where every allowable pair (c. The set of allowable states consists of all pairs (c. O<c<N return one cannibal across the b.3) A (3.3) (2. Possible transitions are: ‘send two cannibals 4.

However there is a only seven crossings as shown in Fig. the boat may be on the other bank. At least eleven crossings are required to transport all ten people safely across the river. cannibals and six of five.3) to (O. To leave the T states without returning. Existence and Uniqueness Certain combinations of N For example. Fig. One must keep in mind the position of the boat. The first move is a forward one. into three classes: T B D and M will nolt be solvable. and four missionaries cannot with a boat holding only two classify the allowable states 1 0 N) 0123+g those along the top (m= L --b w5&!9---Fig. 4). The puzzle of the cannibals and missionaries can now be interpreted as calling for a sequence starting at the upper right comer of the state diagram and terminating at the lower left comer. and then back to the first. where the boat leaves the first bank to travel to the second. with a specific solution. 3 shows the five transitions.Transitions between states can also be shown geometrically on the state diagram. 6 is also the solution to six missionaries with a boat capacity diagonal states. Use forward/backward/forward movements to represent the boat moving from the first bank to the second. 3). 3) backwards are different states. For example. for example. Since the boat only holds two people. 3) to (2. and are not to be mistaken for a similar state already encountered! m 4 5. 4 Backward * tf Fig. 4) will go forwards to (2. (3. The last move is also a forward one since all the cannibals and missionaries have been transported to the second bank. On a state diagram. Thereafter no forward move exists to reach the bottom states. 5 represents the combination of five cannibals and five missionaries with a boat capacity of three. 3) forwards and (2. 2). four cannibals be taken safely across a river people. by traversing along the diagonal of the state diagram. it is impossible to go directly from a state of type T to one of type B. 6 shows the nine crossings needed for the combination of six cannibals and six missionaries with a boat capacity of four. 3) (see Fig. (O< m = c < 3). using the solution involving 7. (2. since the boat can only carry a maximum of two. 5 those along the bottom (m= those along the diagonal O) In cases where the boat capacity is four or mire. Some states cannot be reached directly. any solution of the game must include a stiite of type D. and in some states. any number of cannibals and missionaries can be transported across the river. This signifies the end of the game. with no one to return the boat to its original position. 3) to (1. —— Ruby Lim 137 APL Quote Quad . 3) to (O. Thus. Fig. At most four of the eight people can k transported across the river. 3) must first go through from (3. Fig. The next transition is backwards to (3. a transition from (2. m 4 3 2 1 o c -———g~———+——— 1 01234 Fig.

the first column indicates the bank that the boat is on. Optimal solutions. how to display the output neatly on a page.m 4 6. how to generate the Cannibals and Missionaries 138 APL 92 . which indicated the number of cannibals and missionaries on the first bank. 2 1 0 1 23 ~56 Fig. There is no Iksible solution when the last entry in B cannot generate fi\$iittes that are allowable. 8). the second column indicates the number of cannibals on the current bank. and whether he wants a graphical representation of the output or not. 6 6. I 5 q q q q new allowable states.1 Input m 6 5 a e When the mainline is run. B. e 1 4 3 The size of the output. Put the last entry of B into A. Put them into B (see Fig. also stacked bottom up . two tables are constructed. say N. where B is the position of the boat. using the Apple Macintosh computer. and backward (0) signifies the boat is on the second bank and about to go to the fws~ c and m are as defines earlier. Thus. stores the current solutions. by traversing along the diagonal of the shte diagram. in terms of shortest path. the user is asked to input N and M values that satisfy 1 < M < N. iterate by generating new allowable states. The program will use various functions to generate the new allowable states. A. 4 3 2 1 e 6. and the boat capacity. may be obtained using less straight-forward combinations of top and bottom states. Put the last entry from B into A. and stacking them into B and A. However. when feasible solutions exist. From here. Computer Program The computer program is written in APL68000. Put this in state into B and A. 3. 8). Each table has three columns. 7 During execution. and the other. 3. is large because of the number of them. Feasible solutions always exist for any combination with a boat that holds at least four people. One table. since only one algorithm is needed to travel between banks. 3) is reached. When a state in A and B is one that has already been encountered. The computer model considers three variables. 6. c.2 Solution 0123 456 Fig. ie forward (1) signifies the boat is on the first bank and about to go to the second bank. m). It is more convenient to consider cument bank’s states in the program. Say we start with (1. Note that the game always starts with an equal number of cannibals and missionaries. (B. the number of crossings may not be minimal. the smallest value of N is recommended to be 3 and the smallest value of M is recommended to be 4. (see Fig. 3). the last column indicates the number of missionaries on the current bank. Obviously. so as to make this game look simple and straightforward. These are stacked bottom up. The code involves complex functions which manipulate the input data to calculate how to obtain all the possible states. A feasible solution is found when the state (O. To make the game interesting. remove it from A and B. instructions tell the user to input the number of cannibals and missionaries. the boat capacity M is less than N. stores the partial solutions. unlike the tree diagram and state diagram representations above.

Here are the first four steps of the first shortest path feasible solution. 8 Ed 133 011 CRN 1 SEND RETURN 0 SEND 2 RETURN 1 SEND 8 RETURN i SEND 13 RETURN 1 SEND 2 RETURN 1 SEND 2 MIS 1 1 EI t3 2 1 2 g fZ g a C9N SEND i RETURN Q SEND 2 RETURN 1 SEND Q RETURN 1 SEND 13 RETURN 1 SEND 2 RETURN 8 1 SEND MIS 1 1 D 0 2 1 2 g Ei 1 1 All the shortest path feasible solutions are printed.. The solution is printed out from top down even though the data was stored bottom up as it was produced. 1 CflNNIBfILS 8 MISSIONARIES c Ruby Lim 139 APL Quote Quad . evaluation is implemented depth-wise. and not width-wise. this program is designed to return a simple graphical representation. printed with a graphical representation: ccc Mf-tl 6.E&l” E% 020 E+ 133 Here is a sample output from the case with four cannibals. shortest path feasible solution. and then return a certain number of them. with instructions to send a certain cannibals and/or missionaries across the river. Otherwise. four missionaries and a boat capacity 9f two at most: THERE IS NO FEflS I BLE three SOLUTION missionaries and a boat With three cannibals. capacity of two: THE FEfWIBLE SOLUTIONS CfIN SEND 2 RETURN 1 SEND 2 RETURN 1 SEND 0 RETURN 1 SEND 13 RETURN 1 SEND 2 RETURN Q SEND 1 RRE : MIS B Q 8 ~ J 020 010 011 133 — CfIN SEND 2 RETURN 1 SEND 2 RETURN 1 SEND EI RETURN 1 SEND @ RETURN 1 SEND 2 RETURN 1 SEND 2 IIIS B @ @ Et 2 1 2 1 2 Et 8 g 8 2 El Et 1 1 123 123 020 010 020 133 Fig. provided the user is sali>[ied with only one In terms of a tree diagram. all the shortest path feasible solutions are printed without a graphical representation.3 Output SEND c MMti RETURN cc MPtM 2 Cf?NNIBftLS B MISSIONARIES cc The output prints out all the shortest path solutions to the number of game. where all new states on the same level of the tree are generated before going to the next level. the user will be asked whether he wants graphical output or not. During the input of data. where all new states in a branch are generated before going on to the next branch. If he so desires. Depth-wise evaluation is used in this program because width-wise evaluation involved more complex dlata storage algorithms.

Unfortunately. improved user-interaction can be achieved by actually playing a game with him. which is pretty basic. If the boat capacity is odd. For the more ambitious. print out the first K solutions that have x or less number of crossings. Otherwise. and no output is printed cases because the APL workspace is full! optimal for boat program in some 8. Lockett. Acknowledgements Mr Craig Brown. The graphical representation is prefered visually as the user is able to see each crossing clearly on the screen. the program has relatively little “user friendliness”. execution time needs to be improved to cater for larger problems when the boat capacity is increased. To traverse from the top of the state diagram to the bottom when M <4. and Computers. there are still many of them! For example. However. execution time of the takes a few minutes. where K is a specific number defined by the programmer or the user. especially when there are many feasible solutions. Graphs Academic Press (1970). 5). So. 6). So the code can also be modified to print out only the first K shortest path solutions. “The boat can carry only two people” or “There are more cannibals than missiontiies on the first bank. For instance. However. A. Therefore. by ranking in order of preference for the fewest number of crossings. the first K solutions may not necessarily be optimal in terms of shortest path. one missionary dies”. two vertical jumps are required (see Fig. If his move is illegal. 6. the cannibals are stronger than the missionaries to the extent that the missionaries are safe only when they exceed tie cannibals in number (on both banks and in the boat). Also. this is programmed to print out only the solutions with the shortest path. 1 Thomas Holt Drive. Algorithms. Or. The first shortest path solution is ranked one. where the output does not scroll up every time a new move is made. the boat can only be navigated by a particular combination of cannibals and missionaries. and not so much for large N. Conclusion 1 CfiNNIBRLS \$3 MISSIONARIES cc There is a feasible solution whenever a boat can hold at least four people. However. Reference Cooke and J. at this stage. The next stage in the computer program would be to replace the ‘C’ and ‘M’ with faces or some sort of human figure.4 Problems Execution of the program is slow for large M. this program will output all the solutions in terms of shortest path. K. colour and sound effects may even be included! but most importmtly. Canon Information Systems Research Australia. This means that execution time could still take a while. 9. which may not necessarily be the shortest path. the next shortest path solution is ranked two. Options are available as to the type of output that the user wants. All the feasible solutions still have to be found and compared to the first K shortest path solutions. is to modify the rules. R. This will reduce execution time considerably. this solution may not be optimal in terms of the minimum number of crossings. Interaction occurs only when the user inputs the necessary data. p. the minimum number of crossings is obtained by traversing the diagonal of the state diagram.SEND Mtlfl RETURN c MMM 2 CRNNIBRLS 13 MISSIONf3RIES ccc 7. (x is a specific number defined by the programmer or user). An interactive approach can be developed by actually playing a game with the user. by asking what his next move is.NSW2113. or just one shortest path feasible solution. or the number of cannibals/missionaries is less than twice the boat capacity. and so on. Other variants to make the game more interesting. a boat and some human figures. this is because there are numerous solutions to the game. capacity of three or greater. and every solution is found. 196-212. traversal along the diagonal gives a feasible solution (see Fig. four cannibals. The picture may consist of a river. a message can be returned saying. NorthRyde. L. If the boat can hold an even number of people. four missionaries with a boat capacity of three has 332 shortest path solutions! This program is designed to print out all the solutions with the fewest number of crossings. Other options include printing just one feasible solution. Australia. The code can be modified to print out only the first K solutions. Bellman. Cannibals and Missionaries 140 APL 92 . ie M 24 and 1 c N c 2M. When M 24. For example. a pictorial representation can be designed for the screen.

a. X <y or y = 0. B.zN+l NUMBER OF PEOPLE IN THE HC@T:’ o m+o +((tI>I )Afl<N)/GOINPUT [61 [71 ‘tlUST BE GRERTER THflN 1 BUT LESS THRN CRNNIB(?LS (3ND flISSIONRRIES’ [81 +tlINPUT [91 GOINPUT: ’DO YOU URNT GRAPHICRL OUTPUT? (l=YES.CS[21)) [71 NST+(({-lJ+NST).N) [141 [1s1 INPUT [53 xY+((xY[.TEtlP. GRflPHIC .l) Q+B[~.XY [11 XY+((PPC)P(CSE 13. efficiency for large scale problems has not been achieved.CSC21))-PC [21 xYe((xY[ .0120)’’(xYc.ll)sfl)/[olxY [41 xYe((xY[ .al#D)v(xYc.al \$XYC.Dr Moshe Sniedovich. The Parkville. W-B CRNtl IS. SH9PE . consisting of three functions called INPUT. 196. namely CRNtl I S. NSW 2089.ll 20))/[a3xY [31 xY’\$((xY[. The program consists of eleven functions.NUHBER [11 [21 [31 [41 [51 [63 [71 [81 [91 [101 [111 [121 [131 [141 [151 [161 [171 [181 [19] [2t31 NUtiBER+lEN30 Posscot-tE3 LOOP:B+(NEWST(lTES(B[O.l=(i3. University of Melbourne.X20and Y20and X+Y 20 b.N.Eil Z+t3 ~Pa Y+a 8PEI Z+Z SHRPE TEttP+INE?OfiTeA +LOOP3 GOUTPUT:GRf?PHICefl of this program.I+BIQ. GM.ll#a)). some of which are and these new states satisfy: elementsofPOSSCOMB.[G3B ))/FERS Appendix )/DIFF Computer Code and Comments The computer code is short and precise and was developed with elegance in mind. NST+NEWSTRTES CS. fl.((N+l) [41 Pc+–1 84’1 13JPc POSSCOHB isallthe /N). P O Box I can be Cfl. Unfortunately.Z.=AIEI.al)>NutlBER)/LooP3 +((PR[. I NP UT.N) +(-”/I&(Bn. [11 NINPUT NUIIBER : ‘ PLEWE OF TYPE IN THE CfiNNI BRLS flNC) ttISSION9RIES: ‘ O Netl +( N>2)ntINPuT [21 [31 [41 [51 BE 3 OR GRERTER ‘ ‘ SHOULD +NINPUT tlINPUT: ‘ PLERSE TYPE IN THE POSSCOtlB [11 PC+((3xN+i). the boat.lI)) /[a]xy INITIflLISE:R@+l z+% E@3 Y+i3 Elpo is also an initialization 3P(1.Ell+xY[. contacted at this address. Ruby Lim 141 APLQuote Quad .l))/LOOP +(a/((PR)=PB))/@ LOOP3:+( (B[E3. Y.Ql#B[l. POSSCOtlB . NEWSTRTES . l)P’”CSIB1) From thecurrentstate. Q=NO)’ o GO+CI +((GO>L3)’’GO\$l)/INITIfLISE [101 ‘ERROR: PRESS i OR 0 ONLY’ [111 +GOINPUT [121 [131 possible combinations of thesmtes that satisfy the rules. I NBORT .N)(CSC11. JOIN .@l)<NUtlBER)/CUT 2+2 Y+Y Gwynne Scotford Management Consultants.I +LOOP2 DIFF:B~l 9+1 a\$fl +LOOP3 FERS:+(GO=i)~GOUTPUT +((fJA[.131) B+l O\$B RIO.NEWST9TES .N.X+Y-SM c.].EII+(3XN+1)PIN+1 [31 PCE. Neutral Bay.2)@3 [21 PC[.ll=a)v(xY[. where X = number of cannibals in the boat and Y=numbcr of missionariesin CS is thecurrcnt situation. 010+0 [11 [21 INPUT [31 cl-i +( GO=l)/a [41 [51 OUTPUT C fl N II I S is the mainline GO SHRPE JOIN Z TEflP+INBORTaR +((-ItpZ)<@3)/LOOP3 [211 [221 [231 [241 [251 [261 [271 [281 Z+13 apa +L(J(IP3 CUT:NUtlBER+@l[ . H. usesPOSSCOtlB to generate thenewallowable states. GRINBOflT andOUTPUT . Australia. Mathemi{tics Department.ll+((N+l)/EI). VIC 3150. N.[Q]fI LOOP2:+(fi/(R[EI. llIO. ).NST function./[alxY [61 NST+XY+(PXY)P( (N. Australia. INPUT CM and OIJTPUT.

Q.21 [51 PRINT: (20p((P[0.Qx.9 +((O=ltpZ) ‘W=ltPY)/NOFERS SOLUTIONS RRE: ‘GRRPHICIOIW ‘GRRPHIC A.p ‘IN4-GRINBoRT R.al) fl. GRIN BOfl T is used in producing a graphical tlISSIONRRIES HRUE CROSSED THE RIUER SfJFELY’ GR9PHIC callsGRINBORT and returns a graphical reprcsen tation of the output. 1 (N.Ql>l)~HISS S[1.T ‘ ‘ ‘ ‘ ‘ +(C[I.ll)/’C’ [71 (2\$3p((R[a.Ol).S.ll).~)=R[Q.al).pPca.S.12 [41 A[0.TC calculates the number of people in the boat.TNEW [121 [131 [141 [151 [161 [171 [181 [191 [2F31 [211 [221 [51 R+Y [11 [21 [31 [41 [57 JOIN Ot_DEtlPTY:R+TNEld END: JOIN Z +(Q=ltPY)/YEtlPTY R+Y.[11 [21 [31 [41 [s1 [61 INBORT ‘IN+INBOAT (qB+-1 l\$fl 9X.l+(PTII.QQ.&fl c+w+RB-(PRB)P 6p*sEND lw((pc[.lI+N–R[G.l+(pSII.c.81=l)/ttISS tiISSIONARIES’ VR+ot_f) sH~fJE ‘.’ [61 zoldank~ ‘).lQI+’Y’ TII.’ ‘.91+’ QII.C.R.(N–fl[g. RB+–l 14R Rx+l I\$fq C+f)x+~B-(pRB)p(N.).T.il>l)/LOOP TII. [231 [241 [251 [261 [11 [21 [31 [41 [51 [61 [71 [81 [91 [1(31 [111 [121 [13] 0UTPUT.G)=f)fpRINTzTz Y “ “ PRINTZ:Z sEND NOFEf)S: ’THERE CflN HIS ‘ IS NO FERSIBLE SOLUTION< END: GRINBOflT (Graphical INBORT) calculates the number of people in the boat.21+N-R[Q.121+’ ‘ +(CII.INB INB+GRINt30fIT R [11 LOOP:+RCQ.[0101’ +END YEflPTY:R~Z END : is called when the screen ‘.p.l)/’ SII.l)~’ +Loop END:IN+P.ll=l)/LOOP RII.2])/\$fl’).l .l)/’ TII.’ zo blan~ ‘).N) RETURN ‘ [11 [21 [31 [41 [51 [61 [71 [81 [91 [181 [111 .R.flB.l)PC[.lQ)P~ CANNIBALS.(N–fi[E3.l+(PQII.’ NEH [11 [21 [31 [41 +(Q=ItpOLD)/OLDEttPTY R+OLD. output.l)pP S+((pC[.[UIOI’ ‘.pP[a.ll)/’’).ll 1+-1 LOOP:I+I+l +(12PCK.l)PP IN+P.13)P’ cJeT((Pcc .[DIIJlz is full with feasible solutions.al ReT((PC[ .Q.l)/’ HISS:+(CII.N) P+2 6p’\$jEND RETURN ‘ P+((Pc[ .O]/PRINT [21 [31 RIO.IGNORED+ODL R+l OLR INB+l +Loop END: ’fiLL Q41NB THE CRNNIBRLS RND 1 ‘THE FERSIBLE “ 8+(Py)i_Pz (RC11+2)P’ +(’’/(Q.l)pcc.])/END IN8[Et.g. which is used in producing standard output.llI+’ ‘ TCI.Ol).’ ‘.’ ‘.gl)/END +(CII.O]).21)/’H’ [81 [91 [la] [111 [121 [131 [141 [151 [161 [17] IGNORED4DDL 2 ‘c “ +(A/(g. T+((PC[. This function is an extension to I NE 09 T.+1 pea I.R~.l+(pRII. Cannibals and Missionaries 142 APL 92 .’ +END ‘.