Crossing rivers and bridges: eight puzzles, one algorithmic method

Eight puzzles with precise rules, hints and hidden solutions. From state graphs to BFS and Dijkstra, with a laboratory that checks every crossing.

Section: Algorithms Updated:

Articles /river-bridge-crossing-puzzles-algorithms
Crossing rivers and bridges: eight puzzles, one algorithmic method

20 min

Getting everyone across sounds simple. A small boat, a maximum load, incompatible companions or a torch that must return make it harder. Such problems do not always require a flash of inspiration: we can describe them precisely and systematically search for a route.

Illustration for the Algorithms section
Section: Algorithms

Open the crossing laboratory

Before the moves: define the puzzle properly

For each problem we specify who can row, the boat’s capacity, whether the limit concerns seats or weight, where incompatibilities apply and whether empty trips are allowed. Everyone starts on the initial bank; everyone finishes on the final bank with the boat or torch there too. A return is not free: it is a move, and on the bridge it also takes time.

From a story to a state graph

A state records the location of each item and the transport. In wolf–goat–cabbage, four L/R letters suffice, ordered farmer, wolf, goat, cabbage: the boat always accompanies the farmer. Merely counting people does not distinguish couples, however. On the island each item has three possible locations. An edge is a crossing that satisfies every constraint; we are not just connecting similar pictures.

Not all 16 combinations of four items are legal or reachable. Representing the state correctly comes before choosing the algorithm: a perfect solver of the wrong model answers a different question.

BFS: why it finds the fewest crossings

Breadth-first search uses a queue. It starts at the initial situation, generates legal moves, marks new states when enqueueing them and stores a predecessor for each. First it examines all states one crossing away, then those two crossings away, and so on. On first reaching the goal, no shorter route can remain unexplored. Following predecessors backwards reconstructs the moves.

  1. Put the initial state in the queue and visited set.
  2. Remove the first state. If it is the goal, reconstruct the route.
  3. Generate all legal crossings; enqueue only unvisited states and record where they came from.
  4. Repeat. If the queue empties, the goal is unreachable in this finite model.

Dijkstra: when crossings have different costs

On the bridge, C and D crossing costs 10 minutes, while A returning costs 1. BFS minimizes edges, not minutes. Dijkstra repeatedly chooses the unexamined situation with the least accumulated cost and improves distances when it finds a cheaper route. With nonnegative costs, when the goal’s distance becomes final, its time is minimal. The constraints stay the same: what changes is the order of graph exploration.

Do not confuse the boat’s weight limit with route cost: in the family puzzle kilograms determine whether a move is permitted; they are not minutes and are not added to measure solution quality.

The collection: try before opening the solutions

The tables give one optimal route, not necessarily the only one. In the laboratory you may choose different equivalent people or a different order: a different solution need not be wrong. The island and family puzzles are teaching variants under the rules stated here.

1. Wolf, goat and cabbage

Everyone starts on the starting bank and must reach the other. Only the farmer rows; the boat carries the farmer and at most one other item. On either bank, wolf and goat or goat and cabbage cannot be left together without the farmer. The farmer may return alone. Minimize crossings.

Try this puzzle in the laboratory

A hint without the solution

Which item cannot be left with either of its two neighbours? Why might it need to come back even after reaching the far bank?

Solution, explanation and optimality check

Optimum: 7 crossings.

One computed optimal solution
StepWho crossesRoute
1Farmer, GoatStarting bank → Destination bank
2FarmerDestination bank → Starting bank
3Farmer, WolfStarting bank → Destination bank
4Farmer, GoatDestination bank → Starting bank
5Farmer, CabbageStarting bank → Destination bank
6FarmerDestination bank → Starting bank
7Farmer, GoatStarting bank → Destination bank

The goat must go first. After taking the wolf across, bringing the goat back is not wasteful: it allows the cabbage to cross without leaving a forbidden pair. The shortest route takes 7 crossings. This is a global choice, not a rule of always leaving more items on the destination bank.

2. Three missionaries and three cannibals

Three missionaries and three cannibals must reach the destination bank. The boat carries one or two people, and everyone can row. On either bank and in the boat, cannibals must not outnumber missionaries where any missionaries are present; with no missionaries, any number of cannibals is allowed. Nobody swims and the boat never travels empty. Minimize crossings.

Try this puzzle in the laboratory

A hint without the solution

Checking the destination bank is not enough: what balance between the groups are you leaving behind?

Solution, explanation and optimality check

Optimum: 11 crossings.

One computed optimal solution
StepWho crossesRoute
1Missionary M1, Cannibal C1Starting bank → Destination bank
2Missionary M1Destination bank → Starting bank
3Cannibal C2, Cannibal C3Starting bank → Destination bank
4Cannibal C1Destination bank → Starting bank
5Missionary M1, Missionary M2Starting bank → Destination bank
6Missionary M1, Cannibal C2Destination bank → Starting bank
7Missionary M1, Missionary M3Starting bank → Destination bank
8Cannibal C3Destination bank → Starting bank
9Cannibal C1, Cannibal C2Starting bank → Destination bank
10Missionary M1Destination bank → Starting bank
11Missionary M1, Cannibal C3Starting bank → Destination bank

For equivalent people we can count missionaries and cannibals on one bank and record the boat’s position. The laboratory also distinguishes individuals so they can be selected; this increases the states but does not change the optimum. BFS finds 11 crossings: every situation reachable in fewer crossings is considered before the goal.

3. Three jealous couples

Three couples must reach the other bank with a boat carrying one or two people; anyone may row. Under this traditional puzzle’s constraint, no wife may be with another husband unless her own is present. Check both banks and the passengers in the boat; a wife alone or with only wives is allowed. Passengers disembark together on arrival. Minimize crossings.

Try this puzzle in the laboratory

A hint without the solution

Two groups with the same numbers of husbands and wives need not be equivalent: who is married to whom matters.

Solution, explanation and optimality check

Optimum: 11 crossings.

One computed optimal solution
StepWho crossesRoute
1Husband 1, Wife 1Starting bank → Destination bank
2Husband 1Destination bank → Starting bank
3Wife 2, Wife 3Starting bank → Destination bank
4Wife 1Destination bank → Starting bank
5Husband 2, Husband 3Starting bank → Destination bank
6Husband 2, Wife 2Destination bank → Starting bank
7Husband 1, Husband 2Starting bank → Destination bank
8Wife 3Destination bank → Starting bank
9Wife 1, Wife 2Starting bank → Destination bank
10Wife 1Destination bank → Starting bank
11Wife 1, Wife 3Starting bank → Destination bank

Counting men and women alone is insufficient. The state must retain couple identities; otherwise a program might accept a numerically balanced but forbidden bank. Applying the stated constraint in the boat as well, search finds a minimum of 11 crossings. The constraint belongs to the traditional game, not to a rule of behaviour.

4. Three soldiers and two children

Three soldiers and two children must cross a river. Everyone can row. The boat supports two children or one soldier; a single child is allowed, but a soldier and a child together are not. The boat cannot travel empty and nobody swims. Both children must finish on the destination bank too. Minimize crossings.

Try this puzzle in the laboratory

A hint without the solution

The two children can deliver the boat and bring it back. How can a cycle transfer one soldier?

Solution, explanation and optimality check

Optimum: 13 crossings.

One computed optimal solution
StepWho crossesRoute
1Child R1, Child R2Starting bank → Destination bank
2Child R1Destination bank → Starting bank
3Soldier S1Starting bank → Destination bank
4Child R2Destination bank → Starting bank
5Child R1, Child R2Starting bank → Destination bank
6Child R1Destination bank → Starting bank
7Soldier S2Starting bank → Destination bank
8Child R2Destination bank → Starting bank
9Child R1, Child R2Starting bank → Destination bank
10Child R1Destination bank → Starting bank
11Soldier S3Starting bank → Destination bank
12Child R2Destination bank → Starting bank
13Child R1, Child R2Starting bank → Destination bank

Before a soldier crosses, both children cross and one returns. After the soldier crosses, the other child returns: the soldier stays transferred and the children can repeat the shuttle. Each soldier needs four crossings; after all three have crossed, the children cross together once more. Total 4 · 3 + 1 = 13, matching the BFS optimum.

5. A family and a 100 kg limit

Ada weighs 75 kg, Bruno 65, Carla 40, Diego 30 and Elia 20. Everyone can row and must reach the other bank. The boat supports at most 100 kg: there is no additional two-person limit. Any nonempty group within the weight limit may travel; there are no incompatibilities on the banks. Minimize crossings.

Try this puzzle in the laboratory

A hint without the solution

The last three weigh 90 kg together. The largest group is not necessarily too heavy.

Solution, explanation and optimality check

Optimum: 5 crossings.

One computed optimal solution
StepWho crossesRoute
1Bruno, DiegoStarting bank → Destination bank
2DiegoDestination bank → Starting bank
3Ada, EliaStarting bank → Destination bank
4EliaDestination bank → Starting bank
5Carla, Diego, EliaStarting bank → Destination bank

One solution sends Bruno and Diego, returns Diego, sends Ada and Elia, returns Elia, and finishes with Carla, Diego and Elia. The loads are 95, 30, 95, 20 and 90 kg. BFS certifies the minimum of 5 crossings. Counting people alone would wrongly exclude the final trip with three: weight is the constraint here.

6. Bridge and torch: 1, 2, 5, 10 minutes

Four people take 1, 2, 5 and 10 minutes respectively to cross a bridge. One or two cross at a time, always with the only torch; a pair takes the slower person’s time. Someone must carry the torch back; it cannot be thrown. Everyone starts together and must reach the other side. Minimize total time, not just the number of crossings.

Try this puzzle in the laboratory

A hint without the solution

Should the two slowest cross separately or together? Which return trip needs to have been prepared already?

Solution, explanation and optimality check

Optimum: 17 minutes.

One computed optimal solution
StepWho crossesRouteDurationTotal time
1A · 1 min, B · 2 minStarting bank → Destination bank2 min2 min
2A · 1 minDestination bank → Starting bank1 min3 min
3C · 5 min, D · 10 minStarting bank → Destination bank10 min13 min
4B · 2 minDestination bank → Starting bank2 min15 min
5A · 1 min, B · 2 minStarting bank → Destination bank2 min17 min

A and B cross in 2 minutes; A returns in 1; C and D cross in 10; B returns in 2; A and B cross in 2. Total 2 + 1 + 10 + 2 + 2 = 17 minutes. Always using A as escort would give 2 + 1 + 5 + 1 + 10 = 19 instead. Both strategies have 5 crossings: BFS does not distinguish their times, but Dijkstra does.

7. Using an island as a staging point

Teaching variant: the farmer, wolf, goat and cabbage start on the initial bank and must reach the final bank via an island. Only starting bank–island and island–final bank journeys are allowed, in either direction. Each leg counts as one crossing. The boat carries the farmer and at most one other item. On all three land areas, wolf–goat and goat–cabbage cannot remain together without the farmer. Items may be left on the island. Minimize legs.

Try this puzzle in the laboratory

A hint without the solution

The island is not merely a compulsory stop: it can hold one item while you escort another all the way across.

Solution, explanation and optimality check

Optimum: 10 crossings.

One computed optimal solution
StepWho crossesRoute
1Farmer, GoatStarting bank → Island
2FarmerIsland → Starting bank
3Farmer, WolfStarting bank → Island
4Farmer, WolfIsland → Destination bank
5FarmerDestination bank → Island
6FarmerIsland → Starting bank
7Farmer, CabbageStarting bank → Island
8Farmer, CabbageIsland → Destination bank
9FarmerDestination bank → Island
10Farmer, GoatIsland → Destination bank

One solution first leaves the goat on the island, takes the wolf to the final bank, returns for the cabbage and takes it all the way across; only then is the goat collected from the island. The minimum is 10 legs, not 14: mechanically doubling the original 7 crossings misses the new staging point. The model now retains three possible locations per item and forbids direct travel between the outer banks.

8. Four and four: can it be done?

Four missionaries and four cannibals start on the same bank. The boat carries one or two people and everyone can row. On either bank and in the boat, cannibals must not outnumber missionaries where missionaries are present. The boat never travels empty and nobody swims. Can everyone cross? Do not assume repeating the three-and-three method is enough.

Try this puzzle in the laboratory

A hint without the solution

More people change the graph. When a search fails to reach the goal, has it exhausted all states or merely been stopped early?

Solution, explanation and optimality check

Impossible: all 98 reachable states were examined without finding the goal.

There is no solution under these rules. In the laboratory model, which distinguishes all eight people, BFS examines all 98 states reachable from the start without encountering the goal. This is not a conclusion based on a few failed attempts: the frontier is exhausted. The proof concerns this exact capacity and these constraints; increasing the boat’s capacity creates a different puzzle.

What the algorithm really guarantees

For the constructed graph, BFS takes O(V + E) time and O(V) memory, with V states and E legal moves; generating candidate loads has its own cost too. With a binary priority queue, Dijkstra takes O((V + E) log V). These bounds refer to the graph, which can grow exponentially with the number of items. A safety cutoff therefore does not prove impossibility. Our eight cases are small and are explored exhaustively when required.

Using the laboratory

Choose a puzzle and try moves: an illegal attempt is explained without changing the situation. Undo or restart at any time. A hint searches for one next move on an optimal route from your current situation; Check the optimum compares with the initial state. Opening the solution enters a separate replay, one move at a time, with a table and counters. Close it to recover your game. On the bridge compare the fewest-crossings objective with the least-minutes objective.

Open the crossing laboratory

Going back may be the necessary progress

The common thread is not a sequence to memorize but a method: describe the state, generate legal moves, avoid cycles and choose an optimization criterion. Bringing the boat back need not make a strategy worse. Mathematics distinguishes a locally tempting move from a globally correct and optimal route.

Also explore the weighted-graph laboratory

Further reading