Lesson 5-5 · Implementing compute_bfs_path() — Knowledge ChecksAnswer key
In bfs_distances(), what does `if neighbor not in distance:` mean in pencil terms?
An intersection gets an entry in distance the first time the wave reaches it, so not-in-distance means not-yet-numbered. Blocked intersections never come up at all — they aren't in the graph.
A classmate's walk adds current to the path BEFORE stepping. Their straight-line test, (0,0) to (0,3), prints Steps: 3, but the path is [(0, 0), (0, 1), (0, 2)]. What's wrong?
Adding before stepping writes down where you were, not where you went: the start goes in, and the loop ends on the destination without writing it. The step count is right, which is why a test should check the list, not just its length.
You change pop(0) to pop() in bfs_distances() and Test 4, the wall test, prints Steps: 9 instead of 7. What changed?
Oldest-first (a queue) moves the wave front out one step at a time, so each number is the fewest steps. Newest-first (a stack) numbers along whatever route it happens to dive down, and the walk is only as good as the numbers.
Why is Manhattan given an add_blocked() method that does nothing?
The swap only works if every planner answers the same two calls. Manhattan ignores add_blocked, and the harness shows the consequence: it drives through the wall.