A real map has walls that force a longer route, and sometimes no route at all. Today you pin both edges of BFS, a wall that bends the path into a detour, and a goal sealed off so the search reports no path.
Confirm BFS detours around a wall and returns no path when the goal is unreachable.
Two behaviors separate a real pathfinder from a toy. The first is a detour: when a wall blocks the straight route, breadth-first search still finds the shortest path that exists, even if it is much longer. Here a wall standing between the start and goal forces the path all the way down the left side, across the bottom, and back up the right, seven cells instead of the three a clear row would take.
The second is the dead end. If walls seal the goal off entirely, no flood can reach it, so the came-from map never records it and reconstruction returns nothing. Reporting “no path” cleanly, rather than looping or returning a broken path, is a correctness property worth pinning on its own: a search that cannot admit failure cannot be trusted on success. With both edges nailed down, breadth-first search is complete, and the next chapter generalizes it to maps where steps cost different amounts.
// No new code needed if reconstruct returns nil for an unreachable goal.// Detour: the two goal cells sit across a wall, so the only route is// down the left column, across the bottom, and back up the right column.// Sealed off: the wall column fully separates left from right, so the// flood never reaches the goal and cameFrom has no entry for it.