Ask yourself:
Does DFS guarantee the shortest path in a maze?
What happens if DFS finds a path of length but a path of length exists?
Why does BFS avoid this problem? Next unit explains why BFS is necessary.
DFS visits cells in depth-first order. It might go deep into one corner of the grid before finding B. BFS visits all cells at distance first, then distance , and so on. The first time BFS reaches B, you know no shorter path exists.