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:

  1. Which of DFS or BFS would you use for each problem, and why specifically?
  2. What are the edge cases and the classic correctness traps (visited bookkeeping, recursion depth)?
  3. What are the complexities, and what's the space cost of each?