Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2 B

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

Correct answer: B. 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 20, and subtracting one per blocked cell (a common shortcut) gives 18.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic