Data Structures & AlgorithmsVery hardGATE CS

A 5 × 5 grid is given row by row as .##.. / ..... / ....# / ...#. / .#... ('#' = wall). Moving one step up, down, left or right through open cells, what is the length of a shortest path from the top-left to the bottom-right cell, and how many different shortest paths are there?

  1. A. Length 8, 8 paths
  2. B. Length 10, 6 paths
  3. C. Length 8, 5 paths
  4. D. Length 8, 6 paths

Correct answer: D. Length 8, 6 paths

BFS from the start fixes each cell's distance; ways[cell] = sum of ways of neighbours one step closer. The target is at distance 8 with 6 shortest paths.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic