Problem. Two jugs of capacities and litres, unlimited water, no markings. Allowed moves: fill a jug, empty a jug, pour one into the other until the source is empty or the target is full. Can you measure exactly litres?

The answer

Possible iff and .

Every reachable amount is an integer combination , hence a multiple of (Bézout). Conversely, repeatedly filling and pouring into (emptying when full) realises every multiple of up to .

The famous Die Hard instance: , , . Since , it is solvable.

The constructive strategy

Two symmetric strategies; take the shorter:

Pour : fill ; pour into ; whenever is full, empty it; repeat until some jug holds .
Pour : the mirror image.

int pourSteps(int from, int to, int target) {   // simulate, counting moves
    int x = from, y = 0, steps = 1;             // fill 'from'
    while (x != target && y != target) {
        int t = min(x, to - y);
        x -= t; y += t; steps++;                // pour
        if (x == target || y == target) break;
        if (x == 0) { x = from; steps++; }      // refill
        if (y == to) { y = 0;   steps++; }      // empty
    }
    return steps;
}
int minSteps(int a, int b, int c) {
    if (c > max(a,b) || c % __gcd(a,b)) return -1;
    return min(pourSteps(a, b, c), pourSteps(b, a, c));
}

— each state recurs after at most pours.

BFS on the state space

For three or more jugs, or unusual move sets, model it as a graph and search:

// state = (x, y); moves: fill x, fill y, empty x, empty y, pour x->y, pour y->x
queue<pair<int,int>> q;
map<pair<int,int>, int> dist;

states, 6 moves each. This is the general answer and extends to any number of jugs — see implicit graphs.

The lesson: implicit-graph BFS

The water jug problem is the archetype of a large family:

ProblemStateMoves
Water jugs litresfill, empty, pour
15-puzzleboard configurationslide a tile
Wolf, goat, cabbagewho is on which bankcross with item
Missionaries and cannibalscounts on each bankcross with 1-2 people
Rubik’s cubecube stateface turn
Word laddera wordchange one letter
Coin/token puzzlespositionslegal move
Lock combinationscurrent coderotate a dial

Whenever a puzzle asks for the minimum number of operations, it is a BFS on an implicit graph — and the only real work is defining the state and the neighbour function.

For very large state spaces, bidirectional BFS halves the exponent.

Variants

VariantNote
Minimum number of poursBFS, or compare the two strategies
Three or more jugsBFS; the gcd condition generalises to of all capacities
Jugs with markingsmore moves; still BFS
Infinite supply vs a fixed totalchanges the reachable set
Measure in a specific jugrestrict the goal test
Minimise the water usedDijkstra with pour amounts as weights

Why the gcd appears

The reachable amounts are exactly , and Bézout’s identity says that set is the multiples of . It is the same fact that governs linear Diophantine equations and the Frobenius coin problem — a single number-theoretic statement answering three superficially unrelated puzzles.

See also: BFS · Linear Diophantine Equations · Euclidean Algorithm