Missionaries and Cannibals
Question: 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.
Solution: First let us consider that both the missionaries (M) and cannibals(C) are on the same side of the river. Left Right Initially the positions are : 0M , 0C and 3M , 3C (B) Now let’s send 2 Cannibals to left of bank : 0M , 2C (B) and 3M , 1C Send one cannibal from left to right : 0M , 1C and 3M , 2C (B) Now send the 2 remaining Cannibals to left : 0M , 3C (B) and 3M , 0C Send 1 cannibal to the right : 0M , 2C and 3M , 1C (B) Now send 2 missionaries to the left : 2M , 2C (B) and 1M . 1C Send 1 missionary and 1 cannibal to right : 1M , 1C and 2M , 2C (B) Send 2 missionaries to left : 3M , 1C (B) and 0M , 2C Send 1 cannibal to right : 3M , 0C and 0M , 3C (B) Send 2 cannibals to left : 3M , 2C (B) and 0M , 1C Send 1 cannibal to right : 3M , 1C and 0M , 2C (B)’ Send 2 cannibals to left : 3M , 3C (B) and 0M , 0C • Here (B) shows the position of the boat after the action is performed. Therefore all the missionaries and cannibals have crossed the river safely.
This is the game where you must try to solve the problem with minimum number of attempts :
Python3
print ( "\n" )
print ( "\tGame Start\nNow the task is to move all of them to right side of the river" )
print ( "rules:\n1. The boat can carry at most two people\n2. If cannibals num greater than missionaries then the cannibals would eat the missionaries\n3. The boat cannot cross the river by itself with no people on board" )
lM = 3
lC = 3
rM = 0
rC = 0
userM = 0
userC = 0
k = 0
print ( "\nM M M C C C | --- | \n" )
try :
while ( True ):
while ( True ):
print ( "Left side -> right side river travel" )
uM = int ( input ( "Enter number of Missionaries travel => " ))
uC = int ( input ( "Enter number of Cannibals travel => " ))
if ((uM = = 0 ) and (uC = = 0 )):
print ( "Empty travel not possible" )
print ( "Re-enter : " )
elif (((uM + uC) < = 2 ) and ((lM - uM)> = 0 ) and ((lC - uC)> = 0 )):
break
else :
print ( "Wrong input re-enter : " )
lM = (lM - uM)
lC = (lC - uC)
rM + = uM
rC + = uC
print ( "\n" )
for i in range ( 0 ,lM):
print ( "M " ,end = "")
for i in range ( 0 ,lC):
print ( "C " ,end = "")
print ( "| --> | " ,end = "")
for i in range ( 0 ,rM):
print ( "M " ,end = "")
for i in range ( 0 ,rC):
print ( "C " ,end = "")
print ( "\n" )
k + = 1
if (((lC = = 3 ) and (lM = = 1 )) or ((lC = = 3 ) and (lM = = 2 )) or ((lC = = 2 ) and (lM = = 1 )) or ((rC = = 3 ) and (rM = = 1 )) or ((rC = = 3 ) and (rM = = 2 )) or ((rC = = 2 ) and (rM = = 1 ))):
print ( "Cannibals eat missionaries:\nYou lost the game" )
break
if ((rM + rC) = = 6 ):
print ( "You won the game : \n\tCongrats" )
print ( "Total attempt" )
print (k)
break
while ( True ):
print ( "Right side -> Left side river travel" )
userM = int ( input ( "Enter number of Missionaries travel => " ))
userC = int ( input ( "Enter number of Cannibals travel => " ))
if ((userM = = 0 ) and (userC = = 0 )):
print ( "Empty travel not possible" )
print ( "Re-enter : " )
elif (((userM + userC) < = 2 ) and ((rM - userM)> = 0 ) and ((rC - userC)> = 0 )):
break
else :
print ( "Wrong input re-enter : " )
lM + = userM
lC + = userC
rM - = userM
rC - = userC
k + = 1
print ( "\n" )
for i in range ( 0 ,lM):
print ( "M " ,end = "")
for i in range ( 0 ,lC):
print ( "C " ,end = "")
print ( "| <-- | " ,end = "")
for i in range ( 0 ,rM):
print ( "M " ,end = "")
for i in range ( 0 ,rC):
print ( "C " ,end = "")
print ( "\n" )
if (((lC = = 3 ) and (lM = = 1 )) or ((lC = = 3 ) and (lM = = 2 )) or ((lC = = 2 ) and (lM = = 1 )) or ((rC = = 3 ) and (rM = = 1 )) or ((rC = = 3 ) and (rM = = 2 )) or ((rC = = 2 ) and (rM = = 1 ))):
print ( "Cannibals eat missionaries:\nYou lost the game" )
break
except EOFError as e:
print ( "\nInvalid input please retry !!" )
|
Output
Game Start
Now the task is to move all of them to right side of the river
rules:
1. The boat can carry at most two people
2. If cannibals num greater than missionaries then the cannibals would eat the missionaries
3. The boat cannot cross the river by itself with no people on board
M M M C C C | --- |
Left side -> right side river travel
Enter number of Missionaries travel =>
Invalid input please retry !!
Last Updated :
18 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...