Puzzle | Farmer, Goat, Wolf and Cabbage
There is a farmer who wishes to cross a river, but he is not alone. He also has a goat, a wolf, and a cabbage along with him. There is only one boat available, which can support the farmer and either the goat, wolf, or the cabbage. So at a time, the boat can have only two objects (farmer and one other).

But the problem is,
- If the goat and wolf are left alone (either in the boat or onshore), the wolf will eat the goat.
- Similarly, if the Goat and cabbage are left alone, then goat will eat the cabbage.
The farmer wants to cross the river with all three of his belongings: goat, wolf, and cabbage. What strategy should he use to do so?
Check if you were right - full answer with solution below.
Solution:
First Trip:
- The farmer takes the goat across the river.
- Now: The goat is on the other side, and the farmer returns alone.
- Farmer, wolf, and cabbage are on the starting side; goat is on the far side.
Second Trip:
- The farmer takes the wolf across the river.
- He leaves the wolf on the far side but brings the goat back with him.
- Now: Farmer, goat, and cabbage are on the starting side; wolf is on the far side.
Third Trip:
- The farmer now takes the cabbage across the river.
- He leaves the cabbage with the wolf and returns alone.
- Now: Farmer and goat are on the starting side; cabbage and wolf are on the far side.
Final Trip:
- The farmer takes the goat across the river again.
- Now: All three items — goat, wolf, and cabbage — are on the far side, safely transported.
Solution 2:
This problem can be solved using graph theory . Consider 2 states: initial (A) and final (B). Initially, the right side of the river has nothing on it and the goat, cabbage, and wolf are on the left. And in the final state, all three (goat, cabbage, and wolf) will be on the right side. How can we reach state B from state A? The right bank can have these combinations of goat(G), cabbage(C), wolf(W). -> 0, G, W, C, GW , GC , WC, GWC 0 represents the initial state (A) and GWC represents the final state(B). We can model this problem as an undirected weighted graph. Where each edge in the graph has weight 1 or infinite. This graph can be used to represent our problem.
Now, all paths that have infinite weight cannot be traversed, else the problem's constraints will be violated. So, we have to move from A to B using paths that have weight 1, and we can find a valid path using Dijkstra's Shortest Path algorithm.
Explanation :
It is very clear that initially, the boatman can only take the goat with him. So, from vertex 0 to vertex G, we set the weight to 1. In other cases, (0 to W, 0 to C) someone will get eaten. Hence we set these weights to infinite. Using similar intuition it is easy to devise the solution.
