A river crossing puzzle is called the problem “farmer, wolf, goat, and cabbage cross a river.” It has been mentioned in the folklore of numerous civilizations since at least the 9th century.

The Story Of The River Crossing Problem

A farmer wishes to cross a river, but he is not alone. A goat, a wolf, and a cabbage accompany him. Only one boat can transport the farmer and either the goat, wolf, or cabbage. As a result, the boat can only carry two objects at a time (farmer and one other).

River Crossing Puzzle | Farmer, Wolf, Goat and Cabbage
River Crossing Puzzle | Farmer, Wolf, Goat and Cabbage

But if the goat and Wolf are left alone (in the boat or on land), the wolf will eat the goat. Similarly, if the goat is left alone with the cabbage, the goat will eat the cabbage. The farmer wishes to cross the river with his three belongings: goat, Wolf, and cabbage.

What strategy should he employ?

Solution of River Crossing Puzzle

Putting the Wolf on the other side will keep the goat and cabbage together. Taking away the cabbage will also leave the wolf and goat alone. As a result, the farmer will take the goat on the other side and then return alone. On one side, we have the farmer, wolf, and cabbage; on the other, we have the goat.

He’ll now take the wolf with him, drop the wolf on the other side, and return with the goat. So now we have a farmer, cabbage, and goat on one side and a wolf on the other.

He now takes the cabbage with him and returns alone. So the scenario is now farmer, goat on one side and wolf, cabbage on the other.

Finally, he crosses the river with the goat, bringing all his belongings.

His actions in the solution are summarized in the following steps:

  1. Take the goat over
  2. Return to other side
  3. Take the wolf or cabbage over
  4. Return with the goat
  5. Take the cabbage or wolf over
  6. Return
  7. Take goat over

Thus there are seven crossings, four forward and three back.

River Crossing Puzzle in Python

x = ['M', 'L', 'G', 'C']
y = []

print("Befre Process")
print("Element in the Left Side Bank  ", x)
print("Element in the Right Side Bank  ", y)

while True:
    print(x[1]," ",x[2]," ",x[3],"Select any one from the list")
    i = input("Enter the item: ")
    i=i.upper()
    if x[1]==i and x[2]=='G' and x[3]=='C':
        print("Goat will eat Cabbage")
        break
    elif x[2]==i and x[3]!='C':
        y.append(x[2])
        if len(y)==2 and y[0]=='G':
            x[2]=y[0]
            y[0]=y[1]
            y.pop()
    elif x[1]==i and x[2]=='G':
        y.append(x[1])
        x[1]=x[2]
        x[2]=''
    elif x[1]==i and x[2]=='C':
        y.append(x[1])
        x[1]=x[2]
        x[2]=''
        if len(y)==2 and y[0]=='G':
            x[2]=y[0]
            y[0]=y[1]
            y.pop()
    elif x[1]==i and x[2]!='C' and x[2]!='G':
        y.append(x[1])
        y.append('M')
        x[1]=''
        x=[]
        print("Goal is Reached")
        break
    elif x[2]==i and x[3]=='C':
        y.append(x[2])
        x[2]=x[3]
        x[3]=''
    elif x[3]==i:
        print("Wolf will Eat Goat")
        break
print("After Process")
print("Element in the Left Side Bank  ", x)
print("Element in the Right Side Bank  ", y)

Output

Program Output of River Crossing Puzzle
Output Of River Crossing Puzzle