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?

  1. A. 20
  2. B. 8
  3. C. 4
  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.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic