Q1 — Search: Water Jug BFS

Three move types on the 5-3-2 jug set: BFS level-counting finds 212 to 131 in three pours and proves nothing shorter exists.

The Question

Consider a water-jug puzzle with three jugs a, b and c of capacities 5L, 3L and 2L, respectively.

A state, encoded as a digit string ABC, denotes volume of water in jugs a, b and c, respectively. For example, state 122 denotes that jugs a, b and c contain 1L, 2L and 2L of water, respectively.

The allowable moves (actions) are

  • xETy: pick up jug xx and empty it into jug yy and jug yy is also topped up,
  • xEy: pick up jug xx and empty it into jug yy and jug yy is still not topped up,
  • xTy: pick up jug xx and top up jug yy and jug xx is still not emptied out.

For example: 122→cEa320→bETa500→aTb230122 \xrightarrow{cEa} 320 \xrightarrow{bETa} 500 \xrightarrow{aTb} 230.

MoveGen takes a state ABC and returns a set of neighbours that are one move away from ABC. For example, MoveGen(122)={032,302,320,131}\text{MoveGen}(122) = \{ 032, 302, 320, 131 \}.

#QuestionOfficial answer
1.1Let 212 be the start state and 131 be the goal state, find the shortest path from start to goal. Enter the path starting with 212 as a comma separated list o...212,410,401,131
1.2Let 212 be the start state and 131 be the goal state, find the sequence of moves that produce the shortest path from start to goal. Enter the sequence of mov...cEa,bEc,aTb

The Topic in 60 Seconds

MoveMeaningAmount poured
xEyxEyempty xx into yy, yy NOT topped upall of xx, and yy had room left
xETyxETyempty xx into yy, yy tops outcap(y)−vol(y)\text{cap}(y) - \text{vol}(y)
xTyxTytop up yy from xx, xx keeps somecap(y)−vol(y)\text{cap}(y) - \text{vol}(y)

The three collapse to: pour min⁡(vol(x), cap(y)−vol(y))\min(\text{vol}(x),\ \text{cap}(y)-\text{vol}(y)); the label records which side ran out. BFS explores level by level, so the first arrival at the goal is provably shortest.

Deep-dive: browse the Concepts section for Breadth First Search.

Step 0 — Verify the worked example

122122: a=1, b=2, c=2.

  • cEacEa: c's whole 2L into a (room 4) → a=3, c=0 → 320 ✓
  • bETabETa: top a (room 2) exactly from b's 2L → a=5, b=0 → 500 ✓
  • aTbaTb: top b (needs 1) from a → b=3, a=4... careful: example says 230 — reading it as top-from-full-a: a=5−3=2 → 230 ✓ (that step started from 500).

1.1 — BFS from 212 → 212,410,401,131

Start 212 (a=2, b=1, c=2):

LevelExpandedNew states (producing transfer)
0—212
1212aE/ET/T-b → 032 · bE/T-a → 302 · cE/T-a → 410 · cE/T-b → 203
2032230 (c→a), 302 seen
2302502 (c→a), 212, 032 seen
2410230, 500 (b→a), 401 (bEc / bETc / bTc: b's 1L → c)
2203033 (a→b), 500 (c→a), 230 (c→b)
3…from 401 (a=4,b=0,c=1): top b from a → b=3, a=1 → 131 GOAL

No goal at levels 1–2 ⇒ depth 3 is optimal. Path: 212 → 410 → 401 → 131.

1.2 — Move sequence → cEa,bEc,aTb

MoveFromArithmeticResult
cEa212c's 2L empties into a (room 3, not topped)a=4, c=0 → 410
bEc410b's 1L empties into empty c (not topped)b=0, c=1 → 401
aTb401top b (needs 3) from a's 4L; a keeps 1b=3, a=1 → 131 ✓

Note why the last label is aTb and not aEb: a ends with 1L — not emptied — exactly the T-move signature.

Interactive BFS trace

1 / 5
OPEN = { 212 } (a=2, b=1, c=2)
expandingin OPENsolution pathseen

Tricks & Tips

Answers: 1.1 212,410,401,131 · 1.2 cEa,bEc,aTb