LeetCode 1091 Shortest Path in Binary Matrix - Why Naive Fails

The trap

Try all paths using DFS, tracking the shortest one found.

DFS explores deep before backtracking, potentially visiting many long paths before finding the shortest.

BFS explores level by level, guaranteeing the first path found is the shortest.