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).
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:
- Take the goat over
- Return to other side
- Take the wolf or cabbage over
- Return with the goat
- Take the cabbage or wolf over
- 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," ",x," ",x,"Select any one from the list") i = input("Enter the item: ") i=i.upper() if x==i and x=='G' and x=='C': print("Goat will eat Cabbage") break elif x==i and x!='C': y.append(x) if len(y)==2 and y=='G': x=y y=y y.pop() elif x==i and x=='G': y.append(x) x=x x='' elif x==i and x=='C': y.append(x) x=x x='' if len(y)==2 and y=='G': x=y y=y y.pop() elif x==i and x!='C' and x!='G': y.append(x) y.append('M') x='' x= print("Goal is Reached") break elif x==i and x=='C': y.append(x) x=x x='' elif x==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)