LibraryReference articles
Start with 3. Each move either doubles the number or subtracts one. What is the fewest moves to reach 10? Writing down every number you can reach in one move, then in two, then in three, finds the answer without any cleverness, and also shows when there is no answer at all. The puzzle is a map, and finding the answer is walking it.
Start with the number 3. A move either doubles the current number or subtracts one. How many moves does it take to reach 10?
Guessing is possible, but there is a way that does not depend on insight. Describe the situation by one number, the current value. That number is the state: it holds everything needed to know what moves are available. From each state there are at most two moves. Drawing those as arrows between numbers turns the puzzle into a map of places and roads.
From 3, one move reaches 6 or 2. Those two form the first ring. From 6 the moves lead to 12 or 5, and from 2 to 4 or 1, so the second ring is 12, 5, 4 and 1. The third ring comes from those: 24 and 11 from 12, then 10 and 4 from 5, then 8 and 3 from 4, then 2 and 0 from 1. The first appearance of 10 is in the third ring, via 5. The route reads 3, 6, 5, 10: double, subtract one, double. Three moves.
Because rings are built in order of distance, the first appearance of the goal is the shortest route. Nothing shorter can exist, since every shorter route would have shown up in an earlier ring. This ring-by-ring method is called breadth-first search.
When 3 reappeared in the third ring, it was a state already seen. Exploring it again would repeat work and, in a bigger puzzle, never finish. Keep a list of states already met and skip them. That one habit is what makes the search finish on any problem with a finite number of states.
The craft is in the description. Too little and different situations look identical, so you cannot tell what moves are legal. Too much and you track things that never change the future, and the map becomes huge for no reason. A good state contains exactly what decides the legal moves and whether you have won.
For a puzzle with containers, the state is how much each holds. For a crossing puzzle, it is which side each person or object is on, plus where the light or boat is. For a timer puzzle, it is what is currently running and how much time has elapsed. Writing the state down as a short list is half the work.
Run the rings until no new states appear. If the goal was never met, it is unreachable, and you have proved it by exhaustion, which is sometimes the only proof available. When the map is too big to draw, an invariant, a quantity that no move changes, can give the same conclusion much faster. See Something that never changes, and odd versus even.
Before searching, count the states. If each of three items can be on one of two sides, there are eight states. Counting them, as in Writing out everything that can happen, tells you whether doing the search by hand is sensible or whether you need a shortcut.
Breadth-first search finds the fewest moves, not the cheapest route when moves have different costs. A move that takes ten minutes and a move that takes one minute are both a single arrow here. For cost, the rings have to be ordered by total cost instead, which is a related but different procedure. And the state count can explode: the method is certain but not always practical.
Two checks. Starting from 4 with the same two moves, the fewest moves to reach 7 is two: 4 to 8, then 8 to 7. Listing the first ring, 8 and 3, and then the second, 16, 7, 6 and 2, shows 7 arriving there and not before. And a state already seen should never be explored twice. If either went wrong, redraw the rings slowly.
Name the state: everything that matters about the situation and nothing else. List the legal moves from a state. Then explore in rings, all states one move away, then two, so the first time the goal appears you have the shortest route. Remember states already seen, or the search chases its own tail.
Two coins give four results, not three, and heads then tails is not the same as tails then heads. Counting done by hand, on paper, before a single formula turns up.
A quantity that no allowed move can alter proves some goals impossible without trying them. Odd and even is the simplest such quantity, and surprisingly often the right one.