DSA: Traversing a Grid — DFS vs BFS
Given an MxN grid, explain when you'd use DFS vs BFS for connectivity and for shortest-path problems, with all the edge cases.
Technical Reference & Key Concepts
The Challenge: Grid Traversal Choice
A robot can move up/down/left/right on an MxN grid with obstacles. You need to (a) count connected components and (b) find the minimum steps from a start to a target.
Core questions to address:
- Which of DFS or BFS would you use for each problem, and why specifically?
- What are the edge cases and the classic correctness traps (visited bookkeeping, recursion depth)?
- What are the complexities, and what's the space cost of each?