Data Structures & AlgorithmsVery hardGATE CS
A 5 × 5 grid is given row by row as .##.. / ..... / ....# / ...#. / .#... ('#' = wall). Moving one step up, down, left or right through open cells, what is the length of a shortest path from the top-left to the bottom-right cell, and how many different shortest paths are there?
- A. Length 8, 8 paths
- B. Length 10, 6 paths
- C. Length 8, 5 paths
- D. Length 8, 6 paths
Correct answer: D. Length 8, 6 paths
BFS from the start fixes each cell's distance; ways[cell] = sum of ways of neighbours one step closer. The target is at distance 8 with 6 shortest paths.
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
- What is the minimum number of cuts needed to split 'madamimad' into pieces that are all palindromes?
- A binary min-heap is stored in a 0-indexed array [13, 33, 15, 35, 42, 23, 32]. Perform, in order: insert 6, delete-min, insert 60…
- The directed graph A→C (5), A→E (7), B→A (5), C→B (5), D→A (5), D→B (-6), E→C (9), E→D (7) has one negative edge but no negative cycle…
- Kruskal's algorithm runs on the weighted undirected graph A–C (29), A–F (19), A–G (2), B–D (8), B–G (25), C–G (14), D–F (1), E–F (11), E–G…
- How many structurally different binary trees with 5 nodes have height EXACTLY 3 (height = edges on the longest root-to-leaf path)?
- A BST holds integer keys between 1 and 100. Which sequence CANNOT be the sequence of keys examined while searching for 45?