Data Structures & AlgorithmsHardCodeforces Div 2 D · 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 5306. These codes are dead ends and may never be shown at any moment: 0239, 0336, 0342, 0795, 1736, 1790, 1955, 2524, 2629, 3480, 3998, 4477, 5265, 5840, 5859, 6134, 6207, 6251, 6573, 6718, 7414, 7597, 7767, 7806, 7850, 7896, 7970, 8128, 8542, 9035, 9487, 9841, 9994. 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 + 3 + 0 + 4). 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 tree has vertices with values in brackets: X[-1], L[8], G[-1], S[7], F[8], K[6]. The edges are X–L, L–G, X–S, X–F, S–K. A path is any…
- An undirected graph has the vertices A, G, H, L, P and the edges L–G, H–P, A–H, H–G, H–L, A–P, P–G. How many simple cycles does it contain…
- A grid has 4 rows and 7 columns (top to bottom; '.' open, '#' blocked): .#.##.. ..#..#. .....#. ....... You may only move right or down…
- A directed weighted graph has the arcs u→v (weight): F→K (4), F→U (1), F→H (13), K→F (12), U→F (13), U→K (4), U→L (8), L→F (2), L→K (2)…
- A connected undirected graph has the vertices C, K, L, P, U, X, Y and the edges L–X, X–C, X–Y, C–K, C–P, C–U, K–Y, K–P, Y–U, P–U. Parallel…
- A directed acyclic graph has the vertices C, J, P, U, X and the arcs P→C, P→X, U→C, J→C, C→X. Two players alternately move a single token…