Data Structures & AlgorithmsModerateCodeforces Div 2 C · GATE CS
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 5603. These codes are dead ends and may never be shown at any moment: 0104, 0179, 0530, 0555, 0751, 1259, 1899, 2410, 2463, 2749, 2982, 3121, 3434, 3977, 4313, 4630, 5425, 5489, 5858, 5967, 6339, 6370, 6384, 6448, 6487, 6601, 6790, 6802, 6823, 6924, 7035, 7217, 7728, 7804, 8039, 8476, 8479, 8693, 8788, 8943, 9012, 9436, 9549, 9815, 9872, 9885, 9908. What is the minimum number of moves?
- A. 14
- B. 5
- C. 12
- D. 3
Correct answer: C. 12
BFS over codes that skips the dead ends finds 12 moves; here no dead end lies on a shortest route, so it equals the sum of circular wheel distances (5 + 4 + 0 + 3). 14 turns each wheel without wrapping, 5 is only the largest single-wheel distance, and 3 counts the wheels that move.
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 graph has the vertices A, E, F, M, N, R and the arcs R→F, R→M, A→M, M→R, M→A, E→F, E→N. How many strongly connected components…
- Floyd–Warshall runs on vertices 1 to 4 with these arcs u→v (weight): 1→2 (1), 1→3 (11), 1→4 (12), 2→1 (12), 2→3 (2), 2→4 (8), 3→1 (7), 3→2…
- Two unmarked jugs hold 6 litres and 8 litres. Allowed operations: fill a jug completely, empty a jug completely, or pour one jug into the…
- 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…
- A tree has the vertices B, E, H, I, Q, R, U, V and the edges V–I, I–H, H–R, H–U, R–B, I–E, U–Q. A centroid is a vertex whose removal…
- A tree on the vertices B, C, D, H, J, T, Z has the arcs T→B, Z→T, B→C, B→D, J→D, J→H (each undirected edge is directed as shown). You may…