Three classic grid problems, each with a step-by-step animation: the queue, the expanding frontier, visited cells, and the matching code line. Press play, or step through with the arrow keys.
BFS explores a grid in rings of equal distance from where it started. You keep a queue (first-in, first-out). You pop a cell, look at its neighbours, and push the ones you haven't seen yet. Because you always finish the closer cells before the farther ones, BFS naturally answers "shortest" and "how many layers" questions.
Count connected blobs of land. BFS flood-fills each island so it's counted once.
Rot spreads each minute. Multi-source BFS: all sources start in the queue, layers = time.
Corner to corner in 8 directions. The first time BFS hits the goal is the shortest path.
A different family: recursion that re-asks the same sub-problem over and over. The fix is to write each answer down once and reuse it. Watch the table fill, and watch the cache hits skip work.
Memoization is just recursion + a notebook. Some problems have overlapping sub-problems: the plain recursion calls solve(state) for the same state an exponential number of times. So the first time you finish a state, you store the answer in a cache keyed by that state. Every later time it comes up, you read it back and skip the entire subtree underneath. Each state is computed exactly once.