Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

A grid has 6 rows and 6 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. 246
  2. B. 32
  3. C. 252
  4. D. 16

Correct answer: D. 16

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

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic