Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2 B
A grid has 4 rows and 7 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?
- A. 15
- B. 9
- C. 18
- D. 78
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 84, and subtracting one per blocked cell (a common shortcut) gives 78.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- A directed weighted graph has the arcs u→v (weight): F→K (4), F→U (1), F→H (13), K→F (12), U→F (13), U→K (4), U→L (8), L→F (2), L→K (2)…
- A connected undirected graph has the vertices C, K, L, P, U, X, Y and the edges L–X, X–C, X–Y, C–K, C–P, C–U, K–Y, K–P, Y–U, P–U. Parallel…
- A directed acyclic graph has the vertices C, J, P, U, X and the arcs P→C, P→X, U→C, J→C, C→X. Two players alternately move a single token…
- A grid has 3 rows and 4 columns with these values (top to bottom): 1832 3673 2187 A path moves between side-adjacent cells (up, down, left…
- A tree on the vertices J, L, M, N, T has the arcs T→J, J→N, L→T, M→T (each undirected edge is directed as shown). You may reverse the…
- A grid has 5 rows and 8 columns (rows listed top to bottom; '.' is an open cell, '#' a wall): ..#..... ....#..# ...#...# #.#..... ......#…