Gamified challenge · BFS over states
You have a tap and two unmarked jugs: jug A holds a litres and jug B holds b litres, and both start empty. Each step is one operation: "fill A", "fill B", "empty A", "empty B", "pour A B" or "pour B A" (a pour stops when the source is empty or the destination is full).
Write measure(a, b, target) that returns the fewest operations after which either jug holds exactly target litres, or an empty list if that's impossible. For a = 3, b = 5, target = 4 that takes 6 steps, e.g. ["fill B", "pour B A", "empty A", "pour B A", "fill B", "pour B A"].
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