Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS

A grid has 6 rows and 5 columns (top to bottom; '.' open, '#' blocked): ...#. .#..# ..... ##.#. ..... ..... You may only move right or down, and you cannot enter a blocked cell. How many different routes lead from the top-left cell to the bottom-right cell?

  1. A. 18
  2. B. 9
  3. C. 120
  4. D. 126

Correct answer: B. 9

Let ways[cell] = ways from above + ways from the left, with 0 for blocked cells. The total is 9. Ignoring the blocks would give 126, and subtracting one per blocked cell (a common shortcut) gives 120.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic