Data Structures & AlgorithmsVery hardCodeforces Div 2 D · ICPC regional

Two unmarked jugs hold 7 litres and 8 litres. Allowed operations: fill a jug completely, empty a jug completely, or pour one jug into the other until the source is empty or the destination is full. Both jugs start empty. What is the minimum number of operations needed so that one of the jugs contains exactly 4 litres?

  1. A. 14
  2. B. 6
  3. C. 12
  4. D. 4

Correct answer: C. 12

BFS over (first jug, second jug) states gives 12 operations: repeatedly fill the 7 and pour into the 8, emptying the 8 when full. Requiring the target in the 8-litre jug takes 14; 6 counts only the pours and 4 only the fills of the optimal route.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic