Data Structures & AlgorithmsVery hardGATE 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 5851. These codes are dead ends and may never be shown at any moment: 0108, 2191, 3504, 3762, 3825, 4883, 4921, 5141, 6062, 6398, 6419, 7019, 7491, 8258, 8417, 8424. What is the minimum number of moves?
- A. 19
- B. 5
- C. 13
- D. 15
Correct answer: C. 13
BFS over codes that skips the dead ends finds 13 moves; here no dead end lies on a shortest route, so it equals the sum of circular wheel distances (5 + 2 + 5 + 1). 19 turns each wheel without wrapping, 5 is only the largest single-wheel distance, and 15 assumes a dead end forces a two-move detour.
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 tree has the vertices A, D, E, G, I, L, P, V, W and the edges L–G, L–W, W–P, W–A, A–E, P–D, P–I, L–V. A centroid is a vertex whose…
- A directed acyclic graph has the vertices C, G, K, P, W, X, Z and the arcs Z→X, Z→G, Z→K, C→W, C→K, P→X, P→K, X→W, W→K, G→K. Two players…
- An undirected graph has the vertices H, J, K, N, P, Q, R and the edges K–R, K–P, Q–J, N–R, P–R, N–J, P–J, K–J, Q–R, H–J. How many simple…
- A pile holds p stones. In one move a player removes exactly s stones, where s is one of {2, 3} and s ≤ the pile size. The player who…
- A directed graph has the vertices B, H, L, Q, R, T, U, Z and the arcs B→L, B→H, B→R, B→Q, L→B, L→Z, L→H, Z→B, Z→L, Z→T, H→L, H→U, R→B…
- The directed acyclic graph has the vertices A, I, L, P, Q, W and the arcs W→P, I→A, W→I, L→I, Q→A, L→W, L→Q, L→A. How many distinct…