Gamified challenge · Breadth-first search
A farmer and the items start on the left bank of a river. The boat must carry the farmer, plus at most one item, on every crossing; crossings alternate left → right, right → left, and so on. Each string in conflicts names two items, like "wolf goat", that must never be left on the same bank without the farmer (checked after every crossing).
Write crossing(items, conflicts) that returns the crossings in order: each is the name of the item the farmer takes, or "alone" when the boat carries only the farmer. Everyone, farmer included, must end on the right bank, using the fewest crossings possible; any shortest safe plan passes. If no safe plan exists, return an empty list.
Example: items = ["fox", "hen"], conflicts = ["fox hen"] → ["fox", "alone", "hen"] (or ["hen", "alone", "fox"]).
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google