# Missionaries and Cannibals

• 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.

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 Malprint("\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 numberlC = 3            #lC = Laft side Cannibals numberrM=0            #rM = Right side Missionaries numberrC=0            #rC = Right side cannibals numberuserM = 0        #userM = User input for number of missionaries for right to left side traveluserC = 0        #userC = User input for number of cannibals for right to left travel k = 0print("\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")            breakexcept 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 =>