Data Structures & AlgorithmsVery hardCodeforces 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 9704. These codes are dead ends and may never be shown at any moment: 0046, 0061, 0287, 0340, 0674, 0871, 0983, 1102, 1323, 1706, 1728, 1813, 2232, 2274, 2318, 2555, 2783, 2899, 2911, 3612, 3951, 4175, 4362, 4453, 4557, 5035, 5057, 5261, 5302, 5371, 5427, 5618, 5690, 5809, 6195, 6410, 6479, 6620, 6669, 6783, 6785, 6981, 7147, 7184, 7399, 7426, 7522, 7526, 7589, 7765, 7772, 8321, 8675, 8791, 9012, 9193, 9427, 9624, 9681, 9878. What is the minimum number of moves?
- A. 20
- B. 8
- C. 4
- D. 10
Correct answer: B. 8
BFS over codes that skips the dead ends finds 8 moves; here no dead end lies on a shortest route, so it equals the sum of circular wheel distances (1 + 3 + 0 + 4). 20 turns each wheel without wrapping, 4 is only the largest single-wheel distance, and 10 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 bipartite graph has left side P1, P2, P3, P4 and right side Q1, Q2, Q3, Q4, Q5, with edges P1–Q1, P3–Q1, P3–Q5, P1–Q5, P2–Q3. What is…
- A connected undirected graph has the vertices A, E, G, J, P, Q, Z and the edges Z–P, Z–J, Z–E, Z–A, P–J, P–G, J–E, A–Q. How many bridges…
- An undirected graph has the vertices A, B, J, Q, Y and the edges Q–J, Q–B, Y–J, Y–B, A–J. A walk may repeat vertices and edges. How many…
- Consider this recursive function: python def f(n): if n <= 1: return f(n - 1) if n % 2 == 0: f(n - 3) How many times is f called in total…
- A counter starts at 1. Each step, you may add 1, add 2, or double the counter. Each step must stay at or below 24. Two sequences of steps…
- Floyd–Warshall runs on vertices 1 to 4 with these arcs u→v (weight): 1→4 (4), 2→1 (1), 2→3 (4), 3→1 (5), 3→4 (10), 4→1 (6), 4→2 (6), 4→3…