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?

  1. A. 14
  2. B. 5
  3. C. 12
  4. 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.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic