A river crossing puzzle is the lion, goat, and cabbage problem. It has been mentioned in the folklore of numerous civilizations since at least the 9th century.

You Will Learn

## The Story Of The River Crossing Problem

A farmer wishes to cross a river, but he is not alone. He is accompanied by a goat, a lion, and a cabbage. There is only one boat that can transport the farmer and either the goat, lion, or cabbage. As a result, the boat can only carry two objects at a time (farmer and one other).

But if the goat and lion are left alone (in the boat or on land), the lion 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, lion, and cabbage.

What strategy should he employ?

## Solution of River Crossing Puzzle

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

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

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

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

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

1. Take the goat over
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("Lion will Eat Goat")
break
print("After Process")
print("Element in the Left Side Bank  ", x)
print("Element in the Right Side Bank  ", y)```