Skip to content
Related Articles

Related Articles

Missionaries and Cannibals

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 05 Aug, 2022

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 article is contributed by kaushik. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

This is the game where you must try to solve the problem with minimum number of attempts : 

Python3




#Python program to illustrate Missionaries & cannibals Problem
#This code is contributed by Sunit Mal
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 then missionaries then the cannibals would eat the missionaries\n3. The boat cannot cross the river by itself with no people on board")
lM = 3            #lM = Left side Missionaries number
lC = 3            #lC = Laft side Cannibals number
rM=0            #rM = Right side Missionaries number
rC=0            #rC = Right side cannibals number
userM = 0        #userM = User input for number of missionaries for right to left side travel
userC = 0        #userC = User input for number of cannibals for right to left travel 
k = 0
print("\nM M M C C C |     --- | \n")
try:
    while(True):
        while(True):
            print("Left side -> right side river travel")
            #uM = user input for number of missionaries for left to right travel
            #uC = user input for  number of cannibals for left to right 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 then 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 => Enter number of Cannibals travel => 
Invalid input please retry !!

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!