GATE Computer ScienceHardGATE CS (PYQ pattern)

A 6 × 6 grid is given row by row, where # is a blocked cell: row1 "....#.", row2 "..#...", row3 ".#..#.", row4 "....#.", row5 "#.....", row6 "...#..". From the top-left to the bottom-right cell, each move goes one cell right, one cell down, or one cell diagonally down-right (a diagonal move is allowed even if the two cells beside it are blocked). How many distinct paths avoid all blocked cells?

  1. A. 9
  2. B. 141
  3. C. 170
  4. D. 157

Correct answer: D. 157

DP: P[r][c] = P[r−1][c] + P[r][c−1] + P[r−1][c−1] for open cells and 0 for blocked ones, with P[1][1] = 1. Filling the table row by row gives 157 at the bottom-right. With only right/down moves the count is just 9, so forgetting the diagonal term is the main trap.

More Algorithms questions

Exams that ask this topic