Data Structures & AlgorithmsHardGATE CS · ICPC regional
A four-wheel combination lock shows four digits 0–9 on wheels that wrap around (9 goes to 0 and 0 goes to 9). One move turns one wheel by one step up or down. The lock starts at 0000 and must reach the code 9309. These codes are dead ends and may never be shown at any moment: 0990, 1186, 2061, 2360, 2638, 2691, 3309, 3456, 3769, 3800, 3840, 4144, 4278, 4428, 4479, 4690, 4815, 4817, 5964, 6089, 6184, 6672, 6721, 6773, 7038, 7865, 7964, 8103, 8484, 8830, 8997, 9046, 9109, 9292, 9936, 9987. What is the minimum number of moves?
- A. 5
- B. 7
- C. 6
- D. 4
Correct answer: A. 5
A BFS over the 10 000 codes that skips the dead ends gives 5 moves. Ignoring the dead ends gives 5, and summing the circular distance of each wheel gives 5.
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 grid has 3 rows and 4 columns with these values (top to bottom): 6389 5169 6136 A path moves between side-adjacent cells (up, down, left…
- A directed acyclic graph has the vertices F, H, M, P, U, W and the arcs H→M, H→W, U→F, F→M, F→P, F→W, M→P, M→W. Two players alternately…
- An undirected graph has the vertices A, D, U, W, Y and the edges U–Y, A–D, A–Y, D–Y, W–Y, U–D, A–W, A–U. How many simple cycles does it…
- Floyd–Warshall runs on vertices 1 to 5 with these arcs u→v (weight): 1→2 (6), 1→3 (5), 1→5 (6), 2→3 (5), 2→4 (11), 3→1 (11), 3→2 (3), 3→4…
- An undirected graph on 6 vertices has the edges E–Z, E–K, E–W, Z–K, Z–F, Z–H, Z–W, K–F, K–W. How many simple paths (no repeated vertex) go…
- A directed graph with no directed cycles has arcs u→v (weight) : T→C (8), T→G (-3), T→L (5), C→G (1), C→X (-1), C→L (5), G→X (-3), X→L…