Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS
A grid has 5 rows and 7 columns (rows listed top to bottom; '.' is an open cell, '#' a wall): .#..... ....#.. .#####. .#..... ..#.... Starting at the top-left cell, what is the minimum number of moves to reach the bottom-right cell, where a move goes to one of the four side-adjacent open cells?
- A. 12
- B. 10
- C. 13
- D. 8
Correct answer: A. 12
A BFS over open cells gives 12 moves, the detour forced by the walls. The Manhattan distance 10 ignores walls, and allowing diagonal moves gives 8.
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 connected undirected graph has the vertices D, H, J, K, L, T and the edges J–L, J–D, J–H, D–K, D–T, K–T, T–H. How many bridges does it…
- A tree has the vertices D, E, G, H, I, T, U, X, Z and the edges D–T, D–H, T–G, H–E, E–X, D–Z, T–U, Z–I. What is the minimum number of…
- An undirected graph has the vertices E, J, K, M, X and the edges X–M, X–K, X–J, M–J, K–J, K–E. A walk may repeat vertices and edges. How…
- Cities I, P, S, U, X are joined by roads with these travel costs: U–S 12, U–I 1, U–P 20, U–X 18, S–I 11, S–P 13, S–X 7, I–P 19, I–X 6, P–X…
- An undirected connected graph on 5 vertices has the edges (weights in brackets, all distinct): J–I (26), C–W (25), X–W (27), I–W (24), I–X…
- A tree has vertices with weights in brackets: D[7], N[2], H[11], W[9], L[10], T[5], F[14]. The edges are D–N, N–H, D–W, N–L, W–T, N–F. A…